DataTable.Merge()方法

 

DataTable.Merge()方法可以實現兩張表的數據合併。

table1.Merge(table2)表示將table2中的數據合併到table1中,如:

構建dt1:

DataTable dt1 = new DataTable();

            dt1.Columns.Add("Name", typeof(string));

            dt1.Columns.Add("ID",typeof(string));

            DataRow row1 = dt1.NewRow();

            row1["Name"] = "Francis";

            row1["ID"] = "10001";

            dt1.Rows.Add(row1);

構建dt2

DataTable dt2 = new DataTable();

            dt2.Columns.Add("One", typeof(string));

            dt2.Columns.Add("Two", typeof(string));

            dt2.Columns.Add("Three", typeof(string));

            dt2.Columns.Add("Four", typeof(string));

            DataRow row2 = dt2.NewRow();

            row2[0] = "一";

            row2[1] = "二";

            row2[2] = "三";

            row2[3] = "四";

            dt2.Rows.Add(row2);

執行dt1.Merge(dt2)後得到結果。

執行後dt1會保存合併後的數據。如果此時再執行dt2.Merge(dt1)的話,會得到如下數據:

 

可見,與表相同的列會合並顯示。所示,dt1的數據被移到了右邊。

若合併的表具有相同的主鍵,則合併後主鍵相同並且列相同的數據會被合併。考慮MSDN中的例子:

private static void DemonstrateMergeTable()

        {

            // Demonstrate merging, within and without

            // preserving changes.

 

            // In this example, take these actions:

            // 1. Create a DataTable (table1) and fill the table with data.

            // 2. Create a copy of table1, and modify its data (modifiedTable).

            // 3. Modify data in table1.

            // 4. Make a copy of table1 (table1Copy).

            // 5. Merge the data from modifiedTable into table1 and table1Copy,

            //    showing the difference between setting the preserveChanges

            //    parameter to true and false.

 

            // Create a new DataTable.

            DataTable table1 = new DataTable("Items");

 

            // Add two columns to the table:

            DataColumn column = new DataColumn("id", typeof(System.Int32));

            column.AutoIncrement = true;

            table1.Columns.Add(column);

 

            column = new DataColumn("item", typeof(System.String));

            table1.Columns.Add(column);

 

            // Set primary key column.

            table1.PrimaryKey = new DataColumn[] { table1.Columns[0] };

 

            // Add some rows.

            DataRow row;

            for (int i = 0; i <= 3; i++)

            {

                row = table1.NewRow();

                row["item"] = "Item " + i;

                table1.Rows.Add(row);

            }

 

            // Accept changes.

            table1.AcceptChanges();

            PrintValues(table1, "Original values");

            DataTable dt2 = table1.Copy();

            PrintValues(dt2, "Copy form Table1");

            dt2.Merge(table1,true);

           

            PrintValues(dt2, "dt2 merge table1");

            // Using the same schema as the original table,

            // modify the data for later merge.

            DataTable modifiedTable = table1.Copy();

            foreach (DataRow rowModified in modifiedTable.Rows)

            {

                rowModified["item"] = rowModified["item"].ToString()

                    + " modified";

            }

            modifiedTable.AcceptChanges();

 

            // Change row values, and add a new row:

            table1.Rows[0]["item"] = "new Item 0";

            table1.Rows[1]["item"] = "new Item 1";

 

            row = table1.NewRow();

            row["id"] = 4;

            row["item"] = "Item 4";

            table1.Rows.Add(row);

          //  table1.AcceptChanges();

            // Get a copy of the modified data:

            DataTable table1Copy = table1.Copy();

            PrintValues(table1, "Modified and new Values");

            PrintValues(modifiedTable, "Data to be merged into table1");

 

            // Merge new data into the modified data.

            table1.Merge(modifiedTable, true);

            PrintValues(table1, "Merged data (preserve changes)");

 

            table1Copy.Merge(modifiedTable, false);

            PrintValues(table1Copy, "Merged data (don't preserve changes)");

 

        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章