using (SqlConnection conn = new SqlConnection("server=(local);database=Northwind;user id=sa;password=sa;min pool size=4;max pool size=100;Connection Lifetime=30;"))
{
//创建两个DataTable
SqlDataAdapter adapter1 = new SqlDataAdapter("Select Top 10 * FROM Products", conn);
DataTable table1 = new DataTable();
adapter1.Fill(table1);
SqlDataAdapter adapter2 = new SqlDataAdapter("Select Top 10 * FROM Products order By productid Desc", conn);
DataTable table2 = new DataTable();
adapter2.Fill(table2);
//创建一个新的DataTable并加载前面两个DataTable数据
DataTable table = new DataTable();
table.Load(table1.CreateDataReader());
table.Load(table2.CreateDataReader());
//数据源绑定到GridView
GridView1.DataSource = table.DefaultView;
GridView1.DataBind();
}说明:即使原有DataTable实例中数据结构不一样,使用上边方法仍能正常合并,最终的列数是所有DataTable实例列数的并集!
评论(0)
暂无评论。