如何使用数据表进行多次更新?
我找到了这个Update 1 row
我的代码:
public void ExportCSV(string SQLSyntax, string LeFile, bool Is_Ordre, int TypeDonne)
{
try
{
using (var connectionWrapper = new Connexion())
{
var connectedConnection = connectionWrapper.GetConnected();
SqlDataAdapter da = new SqlDataAdapter(SQLSyntax, connectionWrapper.conn);
DataSet ds = new DataSet();
da.Fill(ds, "Emp");
DataTable dt = ds.Tables["Emp"];
CreateCSVFile(dt, LeFile, Is_Ordre, TypeDonne);
//Update all lines, it not save in Database
foreach (DataRow row in dt.Rows)
{
row["IS_IMPORT"] = true;
}
}
}
catch (Exception excThrown)
{
throw new Exception(excThrown.Message);
}
}
问题是:
foreach (DataRow row in dt.Rows)
{
row["IS_IMPORT"] = true;
}
它不会将其保存到数据库中。
提前谢谢你, 史蒂夫
最佳答案
您需要首先将 DataAdapter 上的 UpdateCommand 属性设置为将执行以更新数据库中的行的 UPDATE 语句。
然后,在更新 DataTable 中的值后,您需要将其传递给 DataAdapter.Update()。 然后,这将为 DataTable 中的每个更新行执行 UpdateCommand。
引用资料:
MSDN - SqlDataAdapter.Update
MSDN-SqlDataAdapter.UpdateCommand
关于C# DataTable 更新多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8850883/