2011-11-04 32 views
1

我正在處理Windows窗體項目,這需要我連接到訪問數據庫並瀏覽記錄,並添加/刪除它們。我在網上查看了一些教程,並認爲我正確地遵循了它們。無論如何,我可以添加一個新項目,並且它會在窗口中顯示正常,但在退出後,所有更改都將丟失,並且數據庫根本不會保存。C# - 無法將更改提交到Access數據庫

任何意見,將不勝感激。以下是我在按下按鈕時用於保存數據庫的代碼。爲了測試目的,我還在一些測試值中硬編碼。

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

回答

2

data.AcceptChanges()重置改性和新的記錄不變的RowState,並從數據集刪除刪除的記錄。通過之前調用它更新你有效地告訴DataAdapter的「沒有任何事情可做」

只是移動電話接受後呼叫改變更新。該呼叫很重要,因爲這樣以後的呼叫更新w.on不會再嘗試進行更改

相關問題