2017-02-09 92 views
0

我正在用OleDBCommandBuilder和類型數據集更新數據庫時遇到問題。當我用準備好的面板編輯我的數據庫時,更新後我看到更改發生在DataGridView中。但是,這些更改是暫時的,因爲當我重新啓動我的應用程序時,更改會從數據庫中取回原始信息。任何人都可以看到下面給出的代碼有問題嗎?C#更新表適配器不更新數據庫

var row = this.dataGridViewProducts.SelectedRows[0]; 
     DataRowView rowView = row.DataBoundItem as DataRowView; 
     ProductsRow productRow = rowView.Row as ProductsRow; 
     if (row != null) 
     { 
      ProductForm formEdit = new ProductForm(ref productRow); 
      bool success = false; 
      while (success == false) 
      { 
       try 
       { 
        formEdit.ShowDialog(); 
        if (this.productsTableAdapter1.Connection.State != ConnectionState.Open) 
         this.productsTableAdapter1.Connection.Open(); 
        var changes = nwindDataSet1.Products.GetChanges(); 
        if (changes != null) 
        { 
         OleDbCommandBuilder builder = new OleDbCommandBuilder(productsTableAdapter1.Adapter); 
         productsTableAdapter1.Adapter.UpdateCommand = builder.GetUpdateCommand(); 
         productsTableAdapter1.Adapter.Update(changes); 
         nwindDataSet1.Products.AcceptChanges(); 
        } 
        success = true; 
       } 
       catch (Exception error) 
       { 
        MessageBox.Show(error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       } 

      } 
     } 

回答