C# - 无法提交对Access DB的更改

时间:2011-11-04 00:21:10

标签: c# database ms-access dataset commit

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

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

    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");
        }

1 个答案:

答案 0 :(得分:2)

data.AcceptChanges()将已修改和新记录上的RowState重置为未更改,并从数据集中删除已删除的记录。通过在更新之前调用,您可以有效地告诉DataAdapter“无需执行任何操作”

更新调用之后,只需移动调用以接受对的更改。该呼叫很重要,因为后续的更新调用方式不会尝试再次进行更改