将 DataRow 从一个 DataTable 复制到另一个 DataTable?

发布于 2024-11-09 05:01:16 字数 1613 浏览 0 评论 0原文

我有一个非常具体的要求。我有两个数据表(都具有相同的架构),一个表(aqTable)有 2 行,另一个表(rapidsTable)有 5 行。我需要检查 aqTable 中的那 2 行是否存在于 rapidsTable 的那 5 行中,如果这两行存在于 rapidsTable 中,则将剩余(差异)3 行复制到 aqTable。

我以这种方式尝试过,但是当它循环第三行时会抛出异常“位置2没有行”,因为其中一个表中的行数不超过2行。

if (rapidsTable.Rows.Count > aqTable.Rows.Count)
{
try
  {
    int aqRow = 0; rows = 0;
    int rCount = rapidsTable.Rows.Count;
    for (int rRow = 0; rRow < rCount; rRow++)
    {
      if (aqTable.Rows.Count == 0)
      {
         DataRow row = aqTable.NewRow();
         row.ItemArray = rapidsTable.Rows[aqRow].ItemArray;
         rowStr += rapidsTable.Rows[rRow][0].ToString() + ",";
         aqTable.Rows.Add(row); rows++;
      }
      else
      {
         string str = aqTable.Rows[aqRow].ToString();
         if (aqTable.Rows.ToString() == null || aqTable.Rows.ToString() == "") 
         { }
         if ((aqTable.Rows[aqRow][1].ToString() == rapidsTable.Rows[rRow][1].ToString()) &&
             (aqTable.Rows[aqRow][2].ToString() == rapidsTable.Rows[rRow][2].ToString()) &&
             (aqTable.Rows[aqRow][3].ToString() == rapidsTable.Rows[rRow][3].ToString()))
         { }
         else
         {
             DataRow row = aqTable.NewRow();
             row.ItemArray = rapidsTable.Rows[aqRow].ItemArray;
             rowStr += rapidsTable.Rows[rRow][0].ToString() + ",";
             aqTable.Rows.Add(row); rRow = rRow - 1; rows++;
          }
      }
     aqRow++;
    }  
catch (Exception ex)
{ throw new Exception("R>AQ Copy: " + ex.Message); }

有没有办法解决这个异常。我从许多不同的博客尝试了许多不同的方法。发现这个博客有一些最好的例子,想分享我的问题。

I have a very specific requirement. I've two datatables (both of same schema) one table(aqTable) with 2 rows and another (rapidsTable) with 5 rows. I need to check if those 2 rows in aqTable are present in those 5 rows of rapidsTable or not, if those two rows are present in rapidsTable then copy remaining (difference) 3 rows to aqTable.

I tried it in this way, but when it loops over for 3rd row throws an exception "There is no row a position2" because there are no more than 2 rows in one of the table.

if (rapidsTable.Rows.Count > aqTable.Rows.Count)
{
try
  {
    int aqRow = 0; rows = 0;
    int rCount = rapidsTable.Rows.Count;
    for (int rRow = 0; rRow < rCount; rRow++)
    {
      if (aqTable.Rows.Count == 0)
      {
         DataRow row = aqTable.NewRow();
         row.ItemArray = rapidsTable.Rows[aqRow].ItemArray;
         rowStr += rapidsTable.Rows[rRow][0].ToString() + ",";
         aqTable.Rows.Add(row); rows++;
      }
      else
      {
         string str = aqTable.Rows[aqRow].ToString();
         if (aqTable.Rows.ToString() == null || aqTable.Rows.ToString() == "") 
         { }
         if ((aqTable.Rows[aqRow][1].ToString() == rapidsTable.Rows[rRow][1].ToString()) &&
             (aqTable.Rows[aqRow][2].ToString() == rapidsTable.Rows[rRow][2].ToString()) &&
             (aqTable.Rows[aqRow][3].ToString() == rapidsTable.Rows[rRow][3].ToString()))
         { }
         else
         {
             DataRow row = aqTable.NewRow();
             row.ItemArray = rapidsTable.Rows[aqRow].ItemArray;
             rowStr += rapidsTable.Rows[rRow][0].ToString() + ",";
             aqTable.Rows.Add(row); rRow = rRow - 1; rows++;
          }
      }
     aqRow++;
    }  
catch (Exception ex)
{ throw new Exception("R>AQ Copy: " + ex.Message); }

Is there a way to solve this for that exception. I tried many different ways from many different blogs. Found this blog has some best examples, thought to share my problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文