Assuming you have configured your data adapter with the following INSERT
statement:
INSERT INTO Categories(CategoryName, Description) VALUES (?, ?)
And now you need the value of the autoincrement key (CategoryID):
Add the following event handler in the class constructor:
daCategory.RowUpdated += new
OleDbRowUpdatedEventHandler(daCategory_RowUpdated);
Which is then defined as:
private void daCategory_RowUpdated(object sender, OleDbRowUpdatedEventArgs e)
{
if (e.StatementType == StatementType.Insert)
{
OleDbCommand cmd = new OleDbCommand("SELECT
@@IDENTITY",daCategory.SelectCommand.Connection);
e.Row["CategoryID"] = (int) cmd.ExecuteScalar();
}
}
So what you're saying is there's a RowUpdated property of the OleDbDataAdapter
(also of the SqlDataAdapter, I assume. Perhaps even of the DataSet?). And you
can wire an event up to that property. And that event can contain code which
can execute a Command object. And I assume that SELECT command looks for a PK.
So it is actually an OleDbCommand that is used to get the newly added PK --
after you've identified both the type of SQL statement that's just been ex ecuted
and the row that has just been changed.
But you now have to test every statement that updates a row in the database, not
to mention creating and executing a command object to retreive the PK value.
I'm wondering if this is any better than simply finding the max value in the PK
column.