2015-09-04 137 views
0

我寫一個adressbook(具有在其中的數據網格視圖當在數據網格改變細胞有可能保存 的變化因此,我有以下代碼:C#和MSACCESS,保存數據網格

private void btnSaveGrid_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     OleDbConnection cn = new OleDbConnection(); 
       OleDbCommand cmd = new OleDbCommand(); 
       cn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myDatabase.accdb;Persist Security Info=False;"; 
       cn.Open(); 

       cmd.Connection = cn; 
       cmd.CommandText = " SELECT * FROM myTable "; 

       OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd.CommandText, cn); 
       OleDbCommandBuilder builder = new OleDbCommandBuilder(dAdapter); 

       dAdapter.Update(dTable); 
       cn.Close(); 
       MessageBox.Show("Information update", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information); 

    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    } 

} 

的dTable是(數據表dTable =新的DataTable();是在InitializeComponent之前writen

現在的問題是,當細胞在DataGrid視圖改變,但數據庫沒有與新的信息更新! 該消息框沒有給出錯誤信息更新。

Iam做錯了什麼?

在此先感謝

+0

您需要創建和填充數據適配器一個時間,而不是在每一個按鈕點擊。請參閱[this](http://contrivedexample.com/2015/03/14/a-basic-example-of-crud-with-datagridview-in-vb-net/)VB.net示例。 – Crowcoder

回答

0

請參閱本文檔: Updating data sources with DataAdapter

首先,您需要編寫自己的更新邏輯,以使其發揮作用。

爲@Crowcoder提到你還需要在做任何更新之前將數據綁定到與填充命令的數據表,當然還有......