如何合并两个DataTable?

解决方案 »

  1.   

    如果結構相同的話,可以用DataSet的Merge方法合并.
      

  2.   

    private void DemonstrateMergeTableAddSchema(){
       // Create a DataSet with one table, two columns, and ten rows.
       DataSet ds = new DataSet("myDataSet");
       DataTable t = new DataTable("Items");   // Add table to the DataSet
       ds.Tables.Add(t);   // Create and add two columns to the DataTable
       DataColumn c1 = new DataColumn("id", Type.GetType("System.Int32"),"");
       c1.AutoIncrement=true;
       DataColumn c2 = new DataColumn("Item", Type.GetType("System.Int32"),"");
       t.Columns.Add(c1);
       t.Columns.Add(c2);   // Set the primary key to the first column.
       t.PrimaryKey = new DataColumn[1]{ c1 };   // Add RowChanged event handler for the table.
       t.RowChanged+= new DataRowChangeEventHandler(Row_Changed);   // Add ten rows.
       for(int i = 0; i <10;i++){
          DataRow r=t.NewRow();
          r["Item"]= i;
          t.Rows.Add(r);
       }   // Accept changes.
       ds.AcceptChanges();
       PrintValues(ds, "Original values");   // Create a second DataTable identical to the first, with
       // one extra column using the Clone method.
       DataTable t2 = t.Clone();
       t2.Columns.Add("extra", typeof(string));   // Add two rows. Note that the id column can't be the 
       // same as existing rows in the DataSet table.
       DataRow newRow;
       newRow=t2.NewRow();
       newRow["id"]= 12;
       newRow["Item"]=555;
       newRow["extra"]= "extra Column 1";
       t2.Rows.Add(newRow);   newRow=t2.NewRow();
       newRow["id"]= 13;
       newRow["Item"]=665;
       newRow["extra"]= "extra Column 2";
       t2.Rows.Add(newRow);   // Merge the table into the DataSet.
       Console.WriteLine("Merging");
       ds.Merge(t2,false,MissingSchemaAction.Add);
       PrintValues(ds, "Merged With Table, Schema Added");
    }private void Row_Changed(object sender, DataRowChangeEventArgs e){
       Console.WriteLine("Row Changed " + e.Action.ToString() + "\t" + e.Row.ItemArray[0]);
    }private void PrintValues(DataSet ds, string label){
       Console.WriteLine("\n" + label);
       foreach(DataTable t in ds.Tables){
          Console.WriteLine("TableName: " + t.TableName);
          foreach(DataRow r in t.Rows){
             foreach(DataColumn c in t.Columns){
                Console.Write("\t " + r[c] );
             }
             Console.WriteLine();
          }
       }
    }
      

  3.   

    .NET Framework 类库   DataSet.Merge 方法
    将指定的 DataSet、DataTable 或 DataRow 对象的数组合并到当前的 DataSet 或 DataTable 中。重载列表
    将 DataRow 对象数组合并到当前的 DataSet 中。[Visual Basic] Overloads Public Sub Merge(DataRow())
    [C#] public void Merge(DataRow[]);
    [C++] public: void Merge(DataRow*[]);
    [JScript] public function Merge(DataRow[]);
    将指定的 DataSet 及其架构合并到当前 DataSet 中。[Visual Basic] Overloads Public Sub Merge(DataSet)
    [C#] public void Merge(DataSet);
    [C++] public: void Merge(DataSet*);
    [JScript] public function Merge(DataSet);
    将指定的 DataTable 及其架构合并到当前 DataSet 中。[Visual Basic] Overloads Public Sub Merge(DataTable)
    [C#] public void Merge(DataTable);
    [C++] public: void Merge(DataTable*);
    [JScript] public function Merge(DataTable);
    将指定的 DataSet 及其架构合并到当前 DataSet 中,在此过程中,将根据给定的参数保留或放弃在此 DataSet 中进行的任何更改。[Visual Basic] Overloads Public Sub Merge(DataSet, Boolean)
    [C#] public void Merge(DataSet, bool);
    [C++] public: void Merge(DataSet*, bool);
    [JScript] public function Merge(DataSet, Boolean);
    将 DataRow 对象数组合并到当前的 DataSet 中,在此过程中,将根据给定的参数保留或放弃在 DataSet 中进行的更改并处理不兼容的架构。[Visual Basic] Overloads Public Sub Merge(DataRow(), Boolean, MissingSchemaAction)
    [C#] public void Merge(DataRow[], bool, MissingSchemaAction);
    [C++] public: void Merge(DataRow*[], bool, MissingSchemaAction);
    [JScript] public function Merge(DataRow[], Boolean, MissingSchemaAction);
    将指定的 DataSet 及其架构与当前的 DataSet 合并,在此过程中,将根据给定的参数保留或放弃在当前 DataSet 中的更改并处理不兼容的架构。[Visual Basic] Overloads Public Sub Merge(DataSet, Boolean, MissingSchemaAction)
    [C#] public void Merge(DataSet, bool, MissingSchemaAction);
    [C++] public: void Merge(DataSet*, bool, MissingSchemaAction);
    [JScript] public function Merge(DataSet, Boolean, MissingSchemaAction);
    将指定的 DataTable 及其架构合并到当前的 DataSet 中,在此过程中,将根据给定的参数保留或放弃在 DataSet 中进行的更改并处理不兼容的架构。[Visual Basic] Overloads Public Sub Merge(DataTable, Boolean, MissingSchemaAction)
    [C#] public void Merge(DataTable, bool, MissingSchemaAction);
    [C++] public: void Merge(DataTable*, bool, MissingSchemaAction);
    [JScript] public function Merge(DataTable, Boolean, MissingSchemaAction);
      

  4.   

    如果两个表结构一样的话,用DataSet.Merge来做还是比较方便的