【问题标题】:dataadapter.update() does not save to databasedataadapter.update() 不保存到数据库
【发布时间】:2015-07-21 10:11:37
【问题描述】:

以下代码不会通过 dataadapter.update() 将数据集的更改保存到数据库。我将winform上的数据显示到文本框。

我有一个保存按钮,可以保存对数据库所做的更改。更改仅保存到数据集的内存副本中。为了将更改保存到数据库,我缺少什么?

public partial class Frm_Main : Form
{
    DataSet ds = new DataSet();
    SqlDataAdapter adapter = new SqlDataAdapter();

    BindingSource binding_Login = new BindingSource();
    SqlCommandBuilder builder = new SqlCommandBuilder();
    SqlConnection connection = new SqlConnection();
    SqlCommand sqlcommand = new SqlCommand();

    public Frm_Main()
    {
        InitializeComponent();

    }

    private void FrmMain_Load(object sender, EventArgs e)
    {
       this.Text = "Main (" + GlobalVars.username.ToString() + ")";
       this.AcceptButton = btnSearch;
       connection.ConnectionString = GlobalVars.sqlConnString;
    }

    private void FrmMain_Close(object sender, EventArgs e)
    {
        Close();
    }

    private void btnSearch_Click(object sender, EventArgs e)
    {

        if(!string.IsNullOrEmpty(txtSearch.Text))
        {
            Search();

        }
    }


    public void Search()
    {
        string sqlcommandstring = "select * from login where loginname like @search;";

        connection.Open();

        sqlcommand.CommandText = sqlcommandstring;
        sqlcommand.Connection = connection;

        sqlcommand.Parameters.AddWithValue("@search", "%" + txtSearch.Text + "%");

        adapter.SelectCommand = sqlcommand ;
        builder.DataAdapter = adapter;

        adapter.Fill(ds,"Login") ;

        BindControls();

        txtLoginName.DataBindings.Add(new Binding("Text", binding_Login, "LoginName"));
        txtPassword.DataBindings.Add(new Binding("Text", binding_Login, "Password"));

        adapter.UpdateCommand = builder.GetUpdateCommand();
        adapter.DeleteCommand = builder.GetDeleteCommand();
        adapter.InsertCommand = builder.GetInsertCommand();
    }

    private void btnNext_Click(object sender, EventArgs e)
    {
        binding_Login.MoveNext();
    }

    protected void BindControls()
    {

        binding_Login.DataSource = ds.Tables[0];

    }

    private void btnPrevious_Click(object sender, EventArgs e)
    {
        binding_Login.MovePrevious();
    }

    private void btnSave_Click(object sender, EventArgs e)
    {

        ds.AcceptChanges();
        adapter.Update(ds.Tables[0]);


    }
}

【问题讨论】:

  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
  • 当您尝试保存时,您确定您的 DataSet 的全局变量实际上已被填充?如果你不调用搜索,它不会有任何数据并且会是空的。

标签: c# dataset dataadapter


【解决方案1】:

我能够通过将保存按钮单击事件更改为以下内容来解决此问题:

private void btnSave_Click(object sender, EventArgs e)
    {
        this.binding_Login.EndEdit();
        adapter.Update(this.ds.Tables[0]);


    }

【讨论】:

    【解决方案2】:

    问题出在这一行:

    private void btnSave_Click(object sender, EventArgs e)
    {
    
        ds.AcceptChanges();//EDIT This is the problem!
        adapter.Update(ds.Tables[0]);
    
    
    }
    

    我遇到了类似的问题,在调试过程中我意识到如果你调用.AcceptChanges()之前DataAdapter.Update(),你所有修改的行都会将它们的状态更改为未更改。这意味着DataAdapter.Update() 将丢失选择正确的INSERT, UPDATE, DELETE 命令所需的所有标志。

    我在使用编辑批处理时也遇到了问题,例如:

    
    row.BeginEdit();
    
    // Modify several rows
    
    row.EndEdit();
    

    据我了解,这里的问题发生是因为所有更改都被保留,直到您调用 AcceptChanges() 方法,从而导致所有行状态标志设置为 Unchanged,使得 DataAdapter.Update() 本质上盲人。

    简而言之:

    • 创建DataAdapter
    • 设置InsertCommand, UpdateCommand, DeleteCommand, SelectCommand
    • 用适配器填写DataSet, DataTable, DataRow[]
    • 更改您的DataSet, DataTable, DataRow[]
    • 确保在修改行的RowState 属性中标记了这些更改
      • 为确保这一点,不要使用批量编辑,也不要在 DataAdapter.Update() 方法之前调用 AcceptChanges()
    • 使用同一个适配器,拨打adapter.Update(DataSet|DataTable|DataRow[])

    【讨论】:

      猜你喜欢
      • 2011-10-13
      • 2020-07-25
      • 2012-04-04
      • 1970-01-01
      • 2013-12-31
      • 1970-01-01
      • 2017-06-16
      • 2018-05-12
      • 1970-01-01
      相关资源
      最近更新 更多