【发布时间】:2018-01-15 18:36:13
【问题描述】:
我在 C# 中使用 MS Access 作为数据库服务器编写了一个数据库客户端应用程序。
用户使用DataGridView 插入新行。我需要在
this.MyTableTableAdapter.Update(MyDataSet.MyTable)
操作。
我不能用
this.MyTableTableAdapter.Fill(MyDataSet.MyTable);
更新操作后刷新整个表,因为需要插入记录的位置丢失了。
所以我读了docs.microsoft.com,“检索身份或自动编号值”部分:
了解如何去做。
他们写了一个模糊的描述:
某些数据库引擎,例如 Microsoft Access Jet 数据库引擎,不支持输出参数并且不能在一个批处理中处理多个语句。使用 Jet 数据库引擎时,您可以通过在 DataAdapter 的 RowUpdated 事件的事件处理程序中执行单独的 SELECT 命令来检索为插入的行生成的新自动编号值。
我还发现了一个必须在事件中执行的代码
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( );
}
}
问题在于 VS IDE 设计器创建了强类型的 DataSet,而没有 DataAdapter 对象的外部可见性。
它创建的 TableAdapter 是从 Component 继承的,而不是从 DataAdapter 继承的。
虽然它在 myTableTableAdapter 类中创建了一个真正的 DataAdapter,但它具有保护级别。
protected internal global::System.Data.OleDb.OleDbDataAdapter Adapter
所以我不能在 myTableTableAdapter 类之外向 Adapter 添加任何事件。
我假设代码应该写在 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>
//------------------------------------------------------------------------------
所以如果我添加任何更改,我的自定义代码可能会丢失。
所以我的问题是 - 如何在强类型数据集中为 DataAdapter 添加 RowUpdated 事件?
【问题讨论】:
-
它是
protected internal,表示protected或internal。您应该能够访问internal字段,只要它与您尝试从中访问的代码位于同一项目(程序集)中 -
是的。 myTable.Adapter.RowUpdated += OnRowUpdated;作品。谢谢。
-
通常自动生成的代码是在
partial类中完成的。如果是这种情况,您可以在另一个文件中添加相关代码,但它仍然在同一个类中。我不知道这里是否是这种情况。但如果是这样,那可能是更好的方法。 -
是的。有两个文件:MyDataSet.Designer.cs 和 MyDataSet.cs 我可以在 MyDataSet.cs public partial class MyTableTableAdapter 中编写代码: global::System.ComponentModel.Component { private void MyMethod() { Adapter.RowUpdated += OnUserRowUpdated;但是如何(从哪里)使 MyMethod() 被调用? private void InitAdapter() { 在 MyDataSet.Designer.cs 文件中。
标签: c# ms-access ado.net auto-increment