DataTable的Merge使用

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        private static void PrintValues(DataTable table)
        {
            string label = table.TableName;
            Console.WriteLine("TableName: " + table.TableName);

            foreach (DataColumn column in table.Columns)
            {
                Console.Write("\t " + column.ColumnName);
            }
            Console.WriteLine();
            foreach (DataRow row in table.Rows)
            {
                foreach (DataColumn column in table.Columns)
                {
                    Console.Write("\t |{0}|", row[column]);
                }
                Console.WriteLine();
            }
        }
        static void Main(string[] args)
        {
            DataTable dtWs = new DataTable("dtWs");
            //DataColumn dcWsWId = new DataColumn("WID", Type.GetType("System.Int32"));//“System.Data.DataException”类型的未经处理的异常在 System.Data.dll 中发生其他信息: < target >.WID 和<source>.WID 擁有衝突的屬性: DataType 屬性不符。
            DataColumn dcWsWId = new DataColumn("WID", Type.GetType("System.String"));
            DataColumn dcWsFId = new DataColumn("FID", Type.GetType("System.String"));
            DataColumn dcBNId = new DataColumn("ADD", Type.GetType("System.String"));
            dtWs.Columns.Add(dcWsWId);
            dtWs.Columns.Add(dcWsFId);
            dtWs.Columns.Add(dcBNId);
            dtWs.PrimaryKey = new DataColumn[] { dcWsWId, dcWsFId };
            for (int i = 0; i < 5; i++)
            {
                DataRow drNew = dtWs.NewRow();
                drNew["WID"] = i;
                drNew["FID"] = "";
                drNew["ADD"] = "BBBB" + i;
                dtWs.Rows.Add(drNew);
            }
            PrintValues(dtWs);

            DataTable dtQry = new DataTable("dtQry");
            DataColumn dcWId = new DataColumn("WID", Type.GetType("System.String"));
            DataColumn dcFId = new DataColumn("FID", Type.GetType("System.String"));
            DataColumn dcNId = new DataColumn("Name", Type.GetType("System.String"));
            dtQry.Columns.Add(dcWId);
            dtQry.Columns.Add(dcFId);
            dtQry.Columns.Add(dcNId);
            dtQry.PrimaryKey = new DataColumn[] { dcWId, dcFId };
            for (int i = 3; i < 8; i++)
            {
                DataRow drNew = dtQry.NewRow();
                drNew["WID"] = i;
                drNew["FID"] = "";
                drNew["Name"] = "AAAA" + i;
                dtQry.Rows.Add(drNew);
            }
            PrintValues(dtQry);

            dtQry.Merge(dtWs, true, MissingSchemaAction.AddWithKey);
            PrintValues(dtQry);

            dtQry.Merge(dtWs, false, MissingSchemaAction.AddWithKey);
            PrintValues(dtQry);

            Console.Read();
        }
    }
}

執行結果:

posted on 2016-11-15 15:58  chen110xi  阅读(5494)  评论(0编辑  收藏  举报