两个DataTable合并

在博客园中发现有很多关于两个DataTable合并的例子,发现大家的方法挺一致,昨天做项目的时候需要合并两个DataTable,我使用的是DataTable.ImportRow ,先记录下,留着以后参考

 

//dt
            DataTable dt = new DataTable();
            dt.TableName = "TestDB";
            dt.Columns.Add("ID", typeof(System.Int32));
            dt.Columns.Add("Name", typeof(System.String));
            dt.Columns.Add("Pwd", typeof(System.String));
            dt.Columns.Add("Age", typeof(System.Int32));
            dt.Rows.Add(1, "111", "123456", 26);
            dt.Rows.Add(2, "222", "7777777", 27);
            dt.Rows.Add(3, "333", "333333", 28);
            //dt1
            DataTable dt1 = new DataTable();
            dt1.TableName = "TestDB";
            dt1.Columns.Add("ID", typeof(System.Int32));
            dt1.Columns.Add("Name", typeof(System.String));
            dt1.Columns.Add("Pwd", typeof(System.String));
            dt1.Columns.Add("Age", typeof(System.Int32));
            dt1.Rows.Add(1, "PC1", "123456", 26);
            dt1.Rows.Add(2, "SHY", "777777", 27);
            dt1.Rows.Add(3, "SZY", "333333", 28);

            //Clone只能复制结构,不能复制数据
            //DataTable dt3 = dt1.Clone();
            foreach (DataRow dr in dt.Rows)
            {
                dt1.ImportRow(dr);//复制操作,DataRow复制到DataTable
            }

posted on 2014-07-22 10:02  忙碌ing  阅读(951)  评论(0)    收藏  举报

导航