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

 

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published.