vb.net ADO CommandBuilder Insert?
I'm using vb.net 2008 and ADO.NET. When using the ADO CommandBuilder and the Insert Command how can I get the newly created Key? (my Table as开发者_开发百科 an Identity column and this is the key.)
Try your insert command somethign like this
insert into table_name values('first value','second value') set @id = SCOPE_IDENTITY()
And then you can set @id Parameter as below
SqlParameter IDParameter =
new SqlParameter("@id",
SqlDbType.Int);
IDParameter.Direction = ParameterDirection.Output;
insertCommand.Parameters.Add(IDParameter);
Finally you can get the value of id parameter in an int variable
int id = (int)IDParameter.Value;
精彩评论