【问题标题】:Update Datatable with another用另一个更新数据表
【发布时间】:2018-08-10 13:32:22
【问题描述】:

我有 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();

【问题讨论】:

    标签: c# linq datatable


    【解决方案1】:

    据我从您上面的架构中可以看出,您只需要编写并执行此 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();
    

    【讨论】:

    • 谢谢,我可以在 SQL 中完成,但我需要在 C# 中使用数据表完成。
    猜你喜欢
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-27
    • 2012-04-07
    • 2014-02-18
    相关资源
    最近更新 更多