B
Bennett Haselton
Suppose I add a new row to a table in a dataset, and then I use an
OleDbDataAdapter to add that new row to a SQL Server database using
OleDbDataAdapter.Update(), as in the following code:
dsLocalDataSet.user_postRow newRow =
dsLocalDataSet1.user_post.Newuser_postRow();
newRow.post_text = this.lblHiddenMessageStorage.Text;
newRow.post_datetime = System.DateTime.Now;
dsLocalDataSet1.user_post.Adduser_postRow(newRow);
this.oleDbUserPostAdapter.Update(dsLocalDataSet1.user_post);
How do I get the ID in the underlying database, of the new row that I
just added?
It might not be the same as the ID of the row that was just added to
the DataSet table. Even if new IDs are generated sequentially in both
the underlying database table and in the DataSet table, and say the
records already in the table had IDs 1, 2, 3, 4, and 5, when I add the
new row to the DataSet table it will get ID 6. However, in the
meantime, some other process might have added a new row to the table
in the underlying database, so that when the row actually gets added
through adapter's Update() method, in the database it will get ID 7.
So I can't just look at the ID of the row in the DataSet.
In MySQL you can do SELECT LAST_INSERT_ID() to get the last
automatically generated value that was inserted into an AUTO_INCREMENT
column by your current connection. Is there an equivalent for SQL
Server? (Or even better, a way to get the value using the built-in
functions of the typed DataSet, so you don't have to get it
inelegantly passing a raw command to SQL Server, which is error-prone
in case you ever want to switch to a different data source type, etc.)
-Bennett
OleDbDataAdapter to add that new row to a SQL Server database using
OleDbDataAdapter.Update(), as in the following code:
dsLocalDataSet.user_postRow newRow =
dsLocalDataSet1.user_post.Newuser_postRow();
newRow.post_text = this.lblHiddenMessageStorage.Text;
newRow.post_datetime = System.DateTime.Now;
dsLocalDataSet1.user_post.Adduser_postRow(newRow);
this.oleDbUserPostAdapter.Update(dsLocalDataSet1.user_post);
How do I get the ID in the underlying database, of the new row that I
just added?
It might not be the same as the ID of the row that was just added to
the DataSet table. Even if new IDs are generated sequentially in both
the underlying database table and in the DataSet table, and say the
records already in the table had IDs 1, 2, 3, 4, and 5, when I add the
new row to the DataSet table it will get ID 6. However, in the
meantime, some other process might have added a new row to the table
in the underlying database, so that when the row actually gets added
through adapter's Update() method, in the database it will get ID 7.
So I can't just look at the ID of the row in the DataSet.
In MySQL you can do SELECT LAST_INSERT_ID() to get the last
automatically generated value that was inserted into an AUTO_INCREMENT
column by your current connection. Is there an equivalent for SQL
Server? (Or even better, a way to get the value using the built-in
functions of the typed DataSet, so you don't have to get it
inelegantly passing a raw command to SQL Server, which is error-prone
in case you ever want to switch to a different data source type, etc.)
-Bennett