Ian Semmel wrote:
The generated INSERT statement in a typed dataset has an INSERT
statement followed by a SELECT statement which gets (I think) the value
of the autoincremented ID.
Question. How do I get hold of this value ?
In case anyone is interested, I finally got this to work
// Copy this code from Dataset.Designe r
string sql = "INSERT INTO ...... "
// BUT replace the SELECT statement in Designer with
+ "SELECT CAST(scope_iden tity() AS int)";
using ( SqlConnection connection = new SqlConnection ( connectionStrin g ) )
{
SqlCommand command = new SqlCommand ( sql, connection );
command.Command Type = System.Data.Com mandType.Text;
// This stuff is in Dataset.Designe r (sort of)
// Copy it changing the command identifier
command.Paramet ers.Add ( new System.Data.Sql Client.SqlParam eter (
"@parmName" , System.Data.Sql DbType.Int, 0,
System.Data.Par ameterDirection .Input, 0, 0, "ColumnName ",
System.Data.Dat aRowVersion.Cur rent, false, null, "", "", "" ) );
.....
command.Paramet ers [ 0 ].Value = ( (int) ( parmValue ) );
.....
connection.Open ();
int ID = (int) command.Execute Scalar ();
}