首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用其他数据表更新数据表

使用其他数据表更新数据表
EN

Stack Overflow用户
提问于 2018-08-10 21:32:23
回答 1查看 235关注 0票数 1

我有两个C#数据表,我希望更新的主数据(Datatable1)有很多行,但第二个只包含几个唯一的行。下面是我正在努力实现的目标。

我已经寻找了一种使用循环和LINQ来实现这一点的方法,但似乎都没有更新ECAD列。

我试过了。

代码语言:javascript
复制
foreach (DataRow row in dtARIAA.Rows)
{
      foreach (DataRow row1 in dtReport.Rows)
      {
            if (row["Ser"] == row1["Ser"] )
            {
                row1["ECAD"] = row["Date"];
            }
      }
}
dtReport.AcceptChanges();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-10 21:48:57

据我所知,根据上面的模式,您只需要编写和执行此SQL。

Table1是包含要更新的日期的表,Table2是包含日期的第二个表

代码语言:javascript
复制
update t1 set t1.ECAD  = [t2].[Date] from Table1 t1 
                         inner join Table2 t2  ON t2.Ser = t1.Ser

但是,如果您想使用内存中已有的两个DataTables,那么您可以使用

代码语言:javascript
复制
// 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();
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51787725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档