定义两个DATATABLE对象,数据结构一样,我想把这两个table所有行合并到新表中,该怎么做呢(生成的datatable是在内存里面,而不是从数据提取的)

解决方案 »

  1.   

    示例如下:下面的控制台应用程序创建一个简单的 DataTable 并将数据添加到该表中。该示例随后创建该表的一个副本,并向该副本添加行。最后,该示例调用 Merge 方法将第二个表中的数据与第一个表中的数据合并。
    private static void 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();
        }
    }
      

  2.   

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