码迷,mamicode.com
首页 > 其他好文 > 详细

DataTable的Merge使用

时间:2016-11-15 16:24:48      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:ram   code   label   .text   iss   for   exception   task   分享   

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使用

标签:ram   code   label   .text   iss   for   exception   task   分享   

原文地址:http://www.cnblogs.com/chen110xi/p/6066103.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!