我有 2 个 C# 数据表,我也想更新的 master(datatable1) 有很多行,但第二个只包含几个唯一的行。以下是我想要实现的目标。
我一直在寻找一种方法来使用循环和 LINQ 来执行此操作,但似乎都无法更新 ECAD 列。
我试过了。
foreach (DataRow row in dtARIAA.Rows)
{
foreach (DataRow row1 in dtReport.Rows)
{
if (row["Ser"] == row1["Ser"] )
{
row1["ECAD"] = row["Date"];
}
}
}
dtReport.AcceptChanges();
最佳答案
据我从上面的模式可以看出,您只需要编写并执行此 SQL。
Table1
是包含要更新日期的表,Table2 是包含日期的第二个表
update t1 set t1.ECAD = [t2].[Date] from Table1 t1
inner join Table2 t2 ON t2.Ser = t1.Ser
但是如果你想使用内存中已有的两个数据表,那么你可以使用
// Loop over the table with the unique Ser value and with the date to transfer
foreach (DataRow r in dtARIAA.Rows)
{
// Get the rows in the destination table with the same "Ser" value
DataRow[] destRows = dtReport.Select("Ser = " + r["Ser"]);
// Now update the destination table rows with the Date value
foreach (DataRow row1 in destRows)
row1["ECAD"] = r["Date"];
}
dtReport.AcceptChanges();
关于c# - 用另一个更新数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51787725/