If your table has a column  that is auto assigned a value when a record is created, e.g. an autoincrement ID value, you can get it using scope_iendity() as follows:

	int RecordId;
	SqlCommand1->CommandType = CommandType::Text;
	SqlCommand1->CommandText = "INSERT INTO MyTable (SomeRow1, SomeRow2) VALUES (@MyValue1, @MyValue2); SELECT CAST(scope_identity() AS int)";
	SqlCommand1->Parameters->AddWithValue("@MyValue1", MyValue1);
	SqlCommand1->Parameters->AddWithValue("@MyValue2", Convert::ToString(MyValue2));
	RecordId = (Int32)SqlCommand1->ExecuteScalar();		//ExecuteScalar returns the auto assigned value for an auto incrment column from the scope_identity() select query


We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.


Your email address will not be published.