C# 无法从.NET C Winforms向MS Access 2000表添加新行

C# 无法从.NET C Winforms向MS Access 2000表添加新行,c#,winforms,ms-access,C#,Winforms,Ms Access,我正在尝试制作一个简单的winform应用程序,在其中我可以读取/更新数据并将数据插入MS Access db表 当我运行应用程序时,它从MS Access db读取数据,我可以添加新数据或编辑现有数据,但这些更改不会发送回db “我的保存”按钮中的代码单击事件 当我按下保存按钮时 我没有收到任何错误消息,在DataGridView中,新行包含值为-1的ID,并且新行未添加到数据库中 有什么问题吗?我错过了什么 当我从MS Access 2007打开mdb文件时,可以在此表中添加新行 这篇文章似

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

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

“我的保存”按钮中的代码单击事件

当我按下保存按钮时

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

有什么问题吗?我错过了什么

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

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

[编辑]


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

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

这是我的密码


如何从MSAccess数据库加载数据?1我已在表单的加载事件中添加了数据源Microsoft Access Databases fileProvider=Microsoft.Jet.OLEDB.4.0 2我有myTableAdapter.FillmyDataSet.MyTable您确定数据集是可更新的吗?您应该检查连接和数据集的参数…我在Winform下面有5个组件-…数据集,…BindingSource,…TableAdapter,tableAdapterManager,…BindingNavigator。此BindingSource组件的参数AllowNew==true。。DataSet组件没有使其可更新或只读的参数。我在哪里可以找到您正在谈论的那些参数?您是定义了TableAdapter中使用的查询,还是使用了默认的自动生成查询?另外,底层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
//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);
    }