简体   繁体   中英

ADO.Net retrieving autoincrement values for MS Access database in strongly typed DataSets

I write a database client application in C# using MS Access as a DB server.

Users insert new rows using a DataGridView . I need to retrieve autoincrement values of the inserted rows on

this.MyTableTableAdapter.Update(MyDataSet.MyTable)

operation.

I can't use

this.MyTableTableAdapter.Fill(MyDataSet.MyTable); 

after update operation to refresh the entire table, because the position of required inserted record is lost.

So I read docs.microsoft.com , section "Retrieving Identity or Autonumber Values":

https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/retrieving-identity-or-autonumber-values

to understand how to do it.

They write a vague description about it:

Some database engines, such as the Microsoft Access Jet database engine, do not support output parameters and cannot process multiple statements in a single batch. When working with the Jet database engine, you can retrieve the new AutoNumber value generated for an inserted row by executing a separate SELECT command in an event handler for the RowUpdated event of the DataAdapter.

I also found a code that must be performed in the event

https://www.safaribooksonline.com/library/view/adonet-cookbook/0596004397/ch04s04.html

private void OnRowUpdated(object Sender, OleDbRowUpdatedEventArgs args)
{
    // Retrieve autonumber value for inserts only.
    if(args.StatementType == StatementType.Insert)
    {
        // SQL command to retrieve the identity value created
        OleDbCommand cmd = new OleDbCommand("SELECT @@IDENTITY", da.SelectCommand.Connection);

        // Store the new identity value to the CategoryID in the table.
        args.Row[CATEGORYID_FIELD] = (int)cmd.ExecuteScalar( );
    }
}

The problem is that VS IDE designer creates strongly typed DataSet without external visibility of DataAdapter object.

It creates TableAdapter that is inherited from Component but not from DataAdapter.

And although it creates a real DataAdapter inside the myTableTableAdapter class, it have protected level.

protected internal global::System.Data.OleDb.OleDbDataAdapter Adapter

So I can't add any event to the Adapter outside the class myTableTableAdapter.

I assume that the code should be written inside the myTableTableAdapter class but the code of this class is autogenerated, and file have next comment

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

So my custom code could be lost if I add any changes.

So my question is - How to add RowUpdated event for the DataAdapter in strongly typed DataSets?

"DataSet, DataTables, and DataRow objects created by the Typed DataSet can be extended through the use of partial classes."

For more on partial classes see 'step 3' here: https://docs.microsoft.com/en-us/aspnet/web-forms/overview/data-access/introduction/creating-a-business-logic-layer-vb#step-3-adding-field-level-validation-to-the-datarow-classes .

The example above uses ColumnChanging but i'm sure you could extend RowUpdated.

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