I am trying to complete a seemingly simple task that has turned into a several hour adventure: Getting @@Identity
from TableAdapter.Insert()
.
One way is to run a select query after the insert command. A good way is to wrap the original command like this:
public int WrapInsert(Parameters)
{
.....
int RowsAffected = this.Insert(..Parameters..);
if ( RowsAffected > 0)
{
try
{
SqlCommand cm = this.Connection.CreateCommand();
cm.CommandText = "SELECT @@IDENTITY";
identity = Convert.ToInt32(cm.ExecuteScalar());
}
finally
{
....
}
}
return RowsAffected;
}