【问题标题】:Can not add new row to MS-Access 2000 table from .NET C# Winforms无法从 .NET C# Winforms 向 MS-Access 2000 表添加新行
【发布时间】:2013-09-18 14:57:13
【问题描述】:

我正在尝试制作一个简单的 winform 应用程序,我可以在其中读取/更新数据并将数据插入到 MS Access db 表中

当我运行应用程序时,它会从 MS Access db 读取数据,我可以添加新数据或编辑现有数据,但这些更改不会发送回 DB。

保存按钮点击事件中的代码

    Validate();
    myBindingSource.EndEdit();

    //myTableAdapterManager.UpdateAll(myDataSet.myTable); //this line was in generated code
    myTableAdapter.Update(myDataSet.myTable); //replaced previous row with this one, but with no effect

当我按下“保存”按钮时

我没有收到任何错误消息,在 DataGridView 中,新行包含带有 -1 值的 ID,并且新行未添加到数据库中

可能是什么问题?我错过了什么?

当我从 MS Access 2007 打开 mdb 文件时,可以在此表中添加一个新行

这篇 SO 帖子似乎是关于同样的问题,但对我的情况没有帮助

unable to add new row using datagridview using C# in Winforms

[编辑]

我打开了 .xsd 文件并为 myTable 添加了插入和更新查询,但这仍然没有帮助 - 当我按下保存按钮时,更改不会发送到数据库

【问题讨论】:

  • 如何从 MSAccess 数据库中加载数据?
  • 1) 我添加了数据源“Microsoft Access 数据库文件”(Provider=Microsoft.Jet.OLEDB.4.0) 2) 在表单的加载事件中我有 myTableAdapter.Fill(myDataSet.myTable)跨度>
  • 您确定您的数据集是可更新的吗?你应该检查你的连接和数据集的参数...
  • 我在 Winform 下面有 5 个组件 - ..DataSet、..BindingSource、..TableAdapter、tableAdapterManager、..BindingNavigator。此 BindingSource 组件具有参数 AllowNew==true。 ..DataSet 组件没有使其可更新或只读的参数。我在哪里可以找到您所说的那些参数?
  • 您是定义了 TableAdapter 中使用的查询还是使用了默认(自动生成的)查询?另外,底层的 db 表是否有主键?

标签: c# winforms ms-access


【解决方案1】:

我找到了如何将数据从网格控件发送到数据库的解决方案,我希望它也对其他人有所帮助(+添加了一些有用的附加内容)

这是我的代码

//form level fields
        OleDbConnection conn = new OleDbConnection();
        OleDbDataAdapter adapter;// = new OleDbDataAdapter();
        DataTable table = new DataTable();
        BindingSource bSource = new BindingSource();

//choosing MS Access file 
        var ecgDbFile = new OpenFileDialog();
        ecgDbFile.InitialDirectory = "c:\\";
        ecgDbFile.Filter = @"MS Access files (*.mdb)|*.mdb";
        ecgDbFile.FilterIndex = 1;
        ecgDbFile.RestoreDirectory = true;

//creating connection
        conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ecgDbFile.FileName;
        conn.Open();

//A nice way how to get a list of Data base's tables
        var restrictions = new string[4];
        restrictions[3] = "Table";
        userTableList = conn.GetSchema("Tables", restrictions);
//then loop through and you can get table names => userTableList.Rows[i][2].ToString()

//reading the data (in the grid)
        adapter = new OleDbDataAdapter("Select * from "+TableName, conn);
        adapter.Fill(table);
        bSource.DataSource = table; //binding through bindingsource
        GridControl.DataSource = bSource; //actually it works fine if GridControl.DataSource is set to table directly as well...

//choose the appropriate trigger (some gridcontrol or button click event) to call for database updates
    private void GridControl_Leave(object sender, EventArgs e)
    {
        //because of this OleDbCommandBuilder TableAdapter knows how to insert/update database
        OleDbCommandBuilder command = new OleDbCommandBuilder(adapter);
        adapter.Update(table);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    • 2011-04-28
    • 1970-01-01
    • 2021-09-11
    相关资源
    最近更新 更多