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做錯了什麼?
在此先感謝
您需要創建和填充數據適配器一個時間,而不是在每一個按鈕點擊。請參閱[this](http://contrivedexample.com/2015/03/14/a-basic-example-of-crud-with-datagridview-in-vb-net/)VB.net示例。 – Crowcoder