ADO:DataSet合并两张表( ds.Merge(ds1))
原文发布时间为:2008-08-01 —— 来源于本人的百度文章 [由搬家工具导入]
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class dataset2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["pubsConn"].ConnectionString);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select title_id,title,type from titles", conn);
DataSet ds = new DataSet();
sda.Fill(ds, "t1");
UniqueConstraint uc = new UniqueConstraint("pk", ds.Tables["t1"].Columns["title_id"], true);
ds.Tables["t1"].Constraints.Add(uc);//表必须设主键,不然会直接合并到表的下面
DataSet ds1 = new DataSet();
sda.SelectCommand=new SqlCommand("select title_id,ord_num from sales",conn);
sda.Fill(ds1,"t1");
Session["ds"]=ds;
Session["ds1"]=ds1;
GridView1.DataSource=ds.Tables["t1"];
GridView1.DataBind();
GridView2.DataSource=ds1.Tables["t1"];
GridView2.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DataSet ds = (DataSet)Session["ds"];
DataSet ds1 = (DataSet)Session["ds1"];
ds.Merge(ds1);//合并
GridView3.DataSource = ds.Tables["t1"];
GridView3.DataBind();
}
}