正文

dataset 合并2007-10-12 09:03:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/jixian/30033.html

分享到:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace merge
{
    class merge
    {
        static void Main(string[] args)
        {
            DataSet regularbooks = new DataSet("RegularBooks");
            DataSet bestsellers = new DataSet("BestSellers");

            DataTable regulartable = new DataTable("books");
            DataTable sellertable = new DataTable("books");

            DataColumn newcol = new DataColumn("ISBN",typeof(string));
            regulartable.Columns.Add(newcol);
            newcol = new DataColumn("Title", typeof(string));
            regulartable.Columns.Add(newcol);

            newcol = new DataColumn("ISBN", typeof(string));
            sellertable.Columns.Add(newcol);
            newcol = new DataColumn("Title", typeof(string));
            sellertable.Columns.Add(newcol);
            newcol = new DataColumn("WeekOnTop10", typeof(int));
            newcol.DefaultValue = 0;
            sellertable.Columns.Add(newcol);
 

            //add book to regular table
            DataRow newbook = regulartable.NewRow();
            newbook["ISBN"] = "154565555";
            newbook["Title"] = ".net compact framework";
            regulartable.Rows.Add(newbook);

          
            newbook = regulartable.NewRow();
            newbook["ISBN"] = "1524455";
            newbook["Title"] = "c#";
            regulartable.Rows.Add(newbook);

            //add book to seller table
            newbook = sellertable.NewRow();
            newbook["ISBN"] = "35446545";
            newbook["Title"] = "lucky man:a memoir";
            newbook["WeekOnTop10"] = 3;
            sellertable.Rows.Add(newbook);

            newbook = sellertable.NewRow();
            newbook["ISBN"] = "35446545";
            newbook["Title"] = "lucky man:a memoir";
            newbook["WeekOnTop10"] = 3;
            sellertable.Rows.Add(newbook);

            regularbooks.Tables.Add(regulartable);
            bestsellers.Tables.Add(sellertable);

            regularbooks.Merge(bestsellers, true, MissingSchemaAction.Add); //合并语句
            foreach (DataRow book in regulartable.Rows)
            {
                Console.WriteLine("{0},ISBN:{1},Weeks On Top 10:{2}",book["Title"].ToString(),book["ISBN"].ToString(),(int)book["WeekOnTop10"]);
            }
        }
    }
}

阅读(2858) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册