SQLAdapter.Update()方法不起作用

时间:2014-04-25 03:22:17

标签: c# sql sql-server

我正在使用SQL Server和C#,并且在向SQL Server数据库表添加/更新数据时遇到问题。

我有一个包含多个表的数据库。我需要将一个表中的某些数据(我不能改变的表(MASTER))复制到另一个表(我已添加到数据库中的表(HELPER))。

这是我的代码。一切似乎都正常工作(没有错误消息),但没有数据添加/更新到目标(HELPER)表。

conn.Open();

//Get the data from the SOURCE table
SqlDataAdapter AdapterSM = new SqlDataAdapter("SELECT lOwnersCorporationID, sPlanNumber FROM OwnersCorporation", conn);
SqlCommandBuilder builder = new SqlCommandBuilder(AdapterSM);
DataSet dsMASTER = new DataSet();
AdapterSM.Fill(dsMASTER, "OwnersCorporation");

//Get the data from the DESTINATION table
SqlDataAdapter AdapterSH = new SqlDataAdapter("SELECT lOwnersCorporationID, sPlanNumber FROM StrataHelper", conn);
SqlCommandBuilder builderSH = new SqlCommandBuilder(AdapterSH);
DataSet dsHELPER = new DataSet();
AdapterSH.Fill(dsHELPER, "StrataHelpera");

DataTable tableMASTER = dsMASTER.Tables["OwnersCorporation"];
DataTable tableHELPER = dsHELPER.Tables["StrataHelpera"];

//Merge the data to the DESTINATION table
tableHELPER.Merge(tableMASTER, true);
tableHELPER.AcceptChanges();
dsHELPER.Tables[0].AcceptChanges();

AdapterSH.DeleteCommand = builderSH.GetDeleteCommand();
AdapterSH.UpdateCommand = builderSH.GetUpdateCommand();
AdapterSH.InsertCommand = builderSH.GetInsertCommand();

//Update DESTINATION table with merged changes
AdapterSH.Update(dsHELPER, "StrataHelpera");
conn.Close();

1 个答案:

答案 0 :(得分:0)

首先你应该删除acceptchanges ...但是即便这样,dsHELPER.Tables [0]中的所有行都会有rowstate == Unchanged所以你的AdapterSH.Update什么都不做......将rowstate更改为Added并且Update调用将触发你的插入声明......

您可能必须更改insert语句以定位正确的表...构建器生成的语句可能会定位源表,这不是您想要的猜测

相关问题