【问题标题】:Join 2 DataTables into 1 in C# [duplicate]在C#中将2个DataTables加入1个[重复]
【发布时间】:2013-08-02 11:01:47
【问题描述】:

我有来自两个不同数据库的两个相同的DataTables(但 ID 是唯一的!)。所以现在我想把这些数据合并成一个DataTable。我不知道该怎么做。

我尝试了以下方法:

DataTable one = new DataTable();
one = baza_win.pobierz_dane("SELECT...");
DataTable two = new DataTable();
two = baza_win2.pobierz_dane("SELECT...");
//DataTable sum = one + two;
DataTable sum = new DataTable();
sum.Clone(one);
sum.Merge(two,false);

但这不适用于sum.Clone(one);

有什么想法吗?

【问题讨论】:

  • 请发布错误信息。

标签: c# datatable


【解决方案1】:
sum = one.Copy();
sum.Merge(two);

【讨论】:

    【解决方案2】:

    我认为你需要复制而不是克隆:

    DataTable one = new DataTable();
    one = baza_win.pobierz_dane("SELECT...");
    DataTable two = new DataTable();
    two = baza_win2.pobierz_dane("SELECT...");
    //DataTable sum = one + two;
    DataTable sum = new DataTable();
    sum.Copy(one);
    sum.Merge(two,false);
    

    【讨论】:

      【解决方案3】:

      复制自链接
      http://msdn.microsoft.com/en-us/library/fk68ew7b.aspx

      重复 how to join two datatable datas into one datatable to show in one gridview in asp.net

      私有静态无效 DemonstrateMergeTable() { DataTable table1 = new DataTable("Items");

          // Add columns
          DataColumn column1 = new DataColumn("id", typeof(System.Int32));
          DataColumn column2 = new DataColumn("item", typeof(System.Int32));
          table1.Columns.Add(column1);
          table1.Columns.Add(column2);
      
          // Set the primary key column.
          table1.PrimaryKey = new DataColumn[] { column1 };
      
          // Add RowChanged event handler for the table.
          table1.RowChanged += 
              new System.Data.DataRowChangeEventHandler(Row_Changed);
      
          // Add some rows.
          DataRow row;
          for (int i = 0; i <= 3; i++)
          {
              row = table1.NewRow();
              row["id"] = i;
              row["item"] = i;
              table1.Rows.Add(row);
          }
      
          // Accept changes.
          table1.AcceptChanges();
          PrintValues(table1, "Original values");
      
          // Create a second DataTable identical to the first.
          DataTable table2 = table1.Clone();
      
          // Add three rows. Note that the id column can't be the  
          // same as existing rows in the original table.
          row = table2.NewRow();
          row["id"] = 14;
          row["item"] = 774;
          table2.Rows.Add(row);
      
          row = table2.NewRow();
          row["id"] = 12;
          row["item"] = 555;
          table2.Rows.Add(row);
      
          row = table2.NewRow();
          row["id"] = 13;
          row["item"] = 665;
          table2.Rows.Add(row);
      
          // Merge table2 into the table1.
          Console.WriteLine("Merging");
          table1.Merge(table2);
          PrintValues(table1, "Merged With table1");
      
      }
      
      private static void Row_Changed(object sender, 
          DataRowChangeEventArgs e)
      {
          Console.WriteLine("Row changed {0}\t{1}", 
              e.Action, e.Row.ItemArray[0]);
      }
      
      private static void PrintValues(DataTable table, string label)
      {
          // Display the values in the supplied DataTable:
          Console.WriteLine(label);
          foreach (DataRow row in table.Rows)
          {
              foreach (DataColumn col in table.Columns)
              {
                  Console.Write("\t " + row[col].ToString());
              }
              Console.WriteLine();
          }
      }
      

      【讨论】:

        【解决方案4】:

        尝试以下...克隆第一个表,使其具有正确的结构,然后将两个表合并到其中。

        DataTable sum = one.Clone()
        sum.Merge(two,false);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-09-11
          • 1970-01-01
          • 2012-10-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多