Getting back sql generated id's

  • Thread starter Thread starter Ted Bogucki
  • Start date Start date
T

Ted Bogucki

I start with an empty dataset
I then add several rows to it
I then update the SQL table with the dataset update method

The table's primary key is an identiy field so the id's are generated by SQL

how do I get the id's that were generated when the records were inserted?
Can I get them loaded back into the dataset?
 
ONe way is to set the Autoincrement property of your dataset to true and
then set the Step to -1. This will ensure that you only send back negative
numbers to SQL SErver which will in turn cause valid numbers only to be
generated by the server. Otherwise, if you use positive numbers, you may
end having users trying to submit the same values. Then you can use an
additional SELECT query or @@Identity to grab the value. Check out
www.betav.com -> Articles -> MSDN -> Managing an @@IDentity Crisis by Bill
Vaughn, he does a great job at walking you through this.

Cheers,

Bill
 
Ted,

You can get the records as output parameters specified on your
InsertCommand.
You need to specify Parameterdirection = out on such parameters and the
relevant SourceColumn before callign DataAdapter.Update.

Once you call DataAdapter.Update with a command setup as above, it will get
repopulate your datarow with the generated key values.

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
 
Back
Top