DataTable合并

时间:2015-04-19 19:16:27
【文件属性】:
文件名称:DataTable合并
文件大小:8.73MB
文件格式:CHM
更新时间:2015-04-19 19:16:27
DataTable合并 //创建数据库连接 SqlConnection con = new SqlConnection("server=.;database = test; uid = sa; pwd = 123456"); try { //打开数据库连接 con.Open(); //数据适配器,传输数据库数据 SqlDataAdapter sda = new SqlDataAdapter("select * from Person where PersonId < 150", con); SqlDataAdapter sda1 = new SqlDataAdapter("select * from Person where PersonId > 150 and PersonId < 160", con); DataSet ds = new DataSet(); sda.Fill(ds, "dt1"); sda1.Fill(ds, "dt2"); //DataTable1 DataTable dt1 = ds.Tables["dt1"]; //DataTable2 DataTable dt2 = ds.Tables["dt2"]; //将DataTable2中的行添加到DataTable1 //前提:dt1和dt2表结构相同 foreach (DataRow dr in dt2.Rows) dt1.Rows.Add(dr.ItemArray); //绑定表格 dataGridView1.DataSource = dt1; } catch (Exception ex) { throw new Exception(ex.Message); } finally { con.Close(); }

网友评论