C#更新表适配器不会更新数据库

时间:2017-02-09 07:57:02

标签: c# .net datagridview oledb strongly-typed-dataset

我在使用OleDBCommandBuilder和类型化数据集更新数据库时遇到问题。当我用准备好的面板编辑我的数据库时,更新后我看到DataGridView中发生了变化。但是这些更改是暂时的,因为当我重新启动应用程序时,更改将被替换为来自数据库的原始信息。任何人都可以看到下面给出的代码有问题吗?

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

            }
        }

1 个答案:

答案 0 :(得分:0)