DataTable的Merge使用

Posted 晨曦歡迎您!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
        }
    }
}

執行結果:

以上是关于DataTable的Merge使用的主要内容,如果未能解决你的问题,请参考以下文章

c#.net 如何将两个datatable拼接成一个Datatable

c#.net 如何将两个datatable拼接成一个Datatable

DataTable的MergeCOPYAcceptChange使用说明

如何将两个DataTable横向合并成一个DataTable

如何使用 .Merge() 方法在合并 DataTables 时覆盖值?

循环 LINQ 查询并将结果附加到 DataTable