【问题标题】:C# - Cannot commit changes to Access DBC# - 无法提交对 Access DB 的更改
【发布时间】:2011-12-21 15:52:53
【问题描述】:

我正在处理一个 Windows 窗体项目,该项目需要我连接到访问数据库并浏览记录,然后添加/删除它们。我在网上看了一些教程,并认为我正确地遵循了它们。无论如何,我可以添加一个新项目,它会在窗口中正常显示,但在我退出后,所有更改都会丢失,并且根本不会保存数据库。

任何建议将不胜感激。以下是我在按下按钮时用来保存数据库的代码。我还硬编码了一些测试值,仅用于测试目的。

    private DataSet data;
    private int inc;
    private int MaxRows;
    private OleDbConnection connect;
    private OleDbDataAdapter da;

    /**
     * Autoloads the first item into our form's text fields.
     */
    private void BookMgmt_Load(object sender, EventArgs e)
    {
        data = new DataSet();
        connect = new System.Data.OleDb.OleDbConnection();
        connect.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=.\\LibraryAppDB.accdb";
        string Books = "SELECT * FROM Books";
        da = new OleDbDataAdapter(Books, connect);
        connect.Open();
        da.Fill(data, "Books");
        Navigate();
        MaxRows = data.Tables["Books"].Rows.Count;
        connect.Close();
        connect.Dispose();
    }

    /**
     *changes data within the array update form with current 
     *item's info
     */
    private void Navigate()
    {
        ID = data.Tables["Books"].Rows[inc];
        textBox1.Text = ID.ItemArray.GetValue(0).ToString();
        textBox2.Text = ID.ItemArray.GetValue(1).ToString();
        textBox3.Text = ID.ItemArray.GetValue(2).ToString();
        textBox5.Text = ID.ItemArray.GetValue(3).ToString();
        textBox6.Text = ID.ItemArray.GetValue(4).ToString();
        textBox7.Text = ID.ItemArray.GetValue(7).ToString();
        textBox8.Text = ID.ItemArray.GetValue(8).ToString();
        textBox10.Text = ID.ItemArray.GetValue(5).ToString();
        textBox11.Text = ID.ItemArray.GetValue(6).ToString();


    }

    /**
     * Saves the new element added into our actual database
     */
    private void saveButton_Click(object sender, EventArgs e)
    {
        OleDbCommandBuilder cb;
        DataRow dRow = data.Tables["Books"].NewRow();
        cb = new OleDbCommandBuilder(da);
        dRow[0] = 11;
        dRow[1] = "test";
        dRow[2] = "test";
        dRow[3] = "test";
        dRow[5] = "test";
        dRow[6] = "test";
        dRow[7] = "test";
        dRow[8] = "test";

        data.Tables["Books"].Rows.Add(dRow);
        data.AcceptChanges();
        da.Update(data, "Books");
        MaxRows = MaxRows + 1;
        inc = MaxRows - 1;
        Navigate();

        MessageBox.Show("Entry Added", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

    /**
     * Deletes the current item displayed on the form
     */
    private void deleteButton_Click(object sender, EventArgs e)
    {
        OleDbCommandBuilder cb;
        cb = new OleDbCommandBuilder(da);
        if (textBox1.Text == "1")
        {
        }
        else
        {
            data.Tables["Books"].Rows[inc].Delete();
            this.MaxRows--;
            this.inc = 0;
            Navigate();
            data.AcceptChanges()
            da.Update(data, "Books");
            MessageBox.Show("Gone");
        }

【问题讨论】:

    标签: c# database ms-access dataset commit


    【解决方案1】:

    data.AcceptChanges() 将已修改记录和新记录的 RowState 重置为未更改,并从数据集中删除已删除的记录。通过在更新之前调用它,您实际上是在告诉 DataAdapter“这里无事可做”

    只需将接受更改的调用移动到更新调用之后。该调用很重要,因为这样后续调用更新不会再次尝试进行更改

    【讨论】:

      猜你喜欢
      • 2023-01-22
      • 2020-06-14
      • 2018-01-20
      • 1970-01-01
      • 1970-01-01
      • 2018-11-22
      • 2013-10-17
      • 2011-08-20
      • 1970-01-01
      相关资源
      最近更新 更多