如何使用DataSet更新数据库?

时间:2011-07-28 19:31:35

标签: .net dataset database-connection sqlcommandbuilder

我正在尝试使用DataSet的内容更新我的数据库,但目前无法使用以下代码执行此操作:

string s = "Select number,name from table where id = 5 and num = 20";

SqlDataAdapter adapter = new SqlDataAdapter(s, con);
adapter.Fill(dset, "ABC");

SqlCommandBuilder sT = new SqlCommandBuilder(adapter);
adapter.Update(dset,"ABC");

此代码未更新数据库中的ABC表。

1 个答案:

答案 0 :(得分:1)

我发现(使用相关的OleDbCommandBuilder)尽管MSDN文档会告诉你,你需要手动设置适配器的InsertCommand,UpdateCommand和DeleteCommand才能使用它们。

        // a is the adapter
        // cb is the commandbuilder
        a.InsertCommand = cb.GetInsertCommand();
        a.DeleteCommand = cb.GetDeleteCommand();
        a.UpdateCommand = cb.GetUpdateCommand();
相关问题