C# SqlDataAdapter.Update()

让人想犯罪 __ 提交于 2020-01-17 07:33:06

问题


Im going crazy with this. I did the ff:

  1. Create a datatable.
  2. Fill it from a SQL db thru SqlDataAdapter.
  3. Edit the datatable thru datagridview.
  4. Call the sqldataadapter.update but the changes are not persisted to the db.

A closer look at the datatable after editing, the row states were not updated even if i actually edited the datatable thru the datagridview but the edited DataRow(s) have changes in the item array. Really confusing.. Any ideas? Thanks.


回答1:


You need several things to make this work. If you drag the table from the Data Sources view you end up with a few different things on your GUI:

  1. a dataSet
  2. a bindingSource
  3. the TableAdapter
  4. a tableAdapterManager
  5. a BindingNavigator

With these in place you can look at the source code to see what hapens beind the scenes. You'll need the EndEdit (as Baldi said before) but you need a little more:

private void UpdateGridView()
{
    // validate that data types corresponds to database table column
    this.Validate();

    // ends edit on the graph table
    this.graphBindingSource.EndEdit();

    // ends edit on the graph table
    this.intervalBindingSource.EndEdit();

    // connect to the database - and exceute changes
    this.tableAdapterManager.UpdateAll(this.diagramDBDataSet);
}

Hopefully this gets you started. If you want to learn more - you can follow this .NET database slide show with the complementary database tutorial. Good luck!




回答2:


Do you use DataBinding? Calling EndEdit may help...!



来源:https://stackoverflow.com/questions/3226932/c-sharp-sqldataadapter-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!