简体   繁体   English

ADO.Net在强类型数据集中检索MS Access数据库的自动增量值

[英]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. 我使用MS Access作为数据库服务器在C#中编写数据库客户端应用程序。

Users insert new rows using a DataGridView . 用户使用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": 因此,我阅读了docs.microsoft.com “检索标识或自动编号值”部分:

https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/retrieving-identity-or-autonumber-values https://docs.microsoft.com/zh-cn/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. 某些数据库引擎(例如Microsoft Access Jet数据库引擎)不支持输出参数,并且不能在单个批处理中处理多个语句。 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. 使用Jet数据库引擎时,可以通过在事件处理程序中为DataAdapter的RowUpdated事件执行单独的SELECT命令来检索为插入的行生成的新AutoNumber值。

I also found a code that must be performed in the event 我还发现了必须在事件中执行的代码

https://www.safaribooksonline.com/library/view/adonet-cookbook/0596004397/ch04s04.html 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. 问题在于VS IDE设计器创建了强类型化的DataSet,而没有DataAdapter对象的外部可见性。

It creates TableAdapter that is inherited from Component but not from DataAdapter. 它创建的TableAdapter是从Component继承的,而不是从DataAdapter继承的。

And although it creates a real DataAdapter inside the myTableTableAdapter class, it have protected level. 尽管它在myTableTableAdapter类内创建了一个真正的DataAdapter,但它具有受保护的级别。

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

So I can't add any event to the Adapter outside the class myTableTableAdapter. 因此,我无法在类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 我假设代码应该写在myTableTableAdapter类内部,但是该类的代码是自动生成的,文件具有下一个注释

//------------------------------------------------------------------------------
// <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? 所以我的问题是-如何在强类型数据集中为DataAdapter添加RowUpdated事件?

"DataSet, DataTables, and DataRow objects created by the Typed DataSet can be extended through the use of partial classes." “可以通过使用部分类来扩展由Typed DataSet创建的DataSet,DataTables和DataRow对象。”

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 . 有关部分类的更多信息,请参见此处的“步骤3”: https : //docs.microsoft.com/zh-cn/aspnet/web-forms/overview/data-access/introduction/creating-a-business-logic-layer- vb#step-3-将字段级别验证添加到数据行类

The example above uses ColumnChanging but i'm sure you could extend RowUpdated. 上面的示例使用ColumnChanging,但我确定您可以扩展RowUpdated。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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