我在使用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);
}
}
}
答案 0 :(得分:0)
尝试阅读文档
https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdataadapter.update(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.updatecommand(v=vs.110).aspx
确保您已赋予UpdateCommand
的{{1}}属性
同时检查DataAdapter