简体   繁体   中英

How can I get the identity of a row that was just added to a DataTable?

Say that I have this code:

rentalEaseDataSet.tblCheckbookEntry.Rows.Add(newRow);

I want to get the identity of a column from that row I just added. How can I do this? Say the row is called ID.

For reference, I'm using C# and sql server express.

Well, that row hasn't actually been saved to the database yet, so it doesn't have a database id (so sql server express isn't an issue). You need to use the adapter to save the changes, and then the id of newRow should reflect the database.

Assuming you are dealing with an auto-incremented identity value on the source of the data, you can't do this. You have to commit the changes to the data source through a data adapter, and then the column that represents the auto incremented id will be updated with the value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM