Combine Three tables within dataset
How to combine two tables in dataset and make it singleExample..
My dataset has records in ds.Tables[0] and ds.Tables[1] and ds.Tables[2].
I want all these records in single dataset(ds.Tables[0])
adapter.Fill(ds,"Table(1)"); //ds is dataset and table name is the next parameter
adapter.Fill(ds,"Table(2)");
ds.Tables[2].Merge(ds.Tables[1]);
For Ex:
DataSet ds1 = new DataSet();
DataSet ds2 = new DataSet();
SqlDataAdapter da= new SqlDataAdapter();
da= new SqlDataAdapter("SELECT * FROM FirstTable", Conn);
da.Fill(ds1, "Table");
da= new SqlDataAdapter("SELECT * FROM SecondTable", Conn);
da.Fill(ds2, "Table");
ds1.Merge(ds2);
private void BindGridWithMergeTables()
{
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
dt1.Columns.Add("ID");
dt2.Columns.Add("ID");
DataRow dr1 = dt1.NewRow();
DataRow dr2 = dt2.NewRow();
dr1["ID"] = "1";
dr2["ID"] = "2";
dt1.Rows.Add(dr1);
dt2.Rows.Add(dr2);
DataTable dtAll = new DataTable();
dtAll = dt1.Copy();
dtAll.Merge(dt2, true);
dataGridView1.DataSource = dtAll;
dataGridView1.DataBind();
}
ds.Tables[0].Merge(ds.Tables[1]);
ds.Tables[0].Merge(ds.Tables[2]);
--Then Use
ds.Tables[0] ;
Or
DataTable dt = ds.Tables[0];
private void DemonstrateMerge()
{
DataSet dataSet = new DataSet("dataSet");
DataTable table = new DataTable("Product");
DataColumn idColumn = new DataColumn("id",
Type.GetType("System.Int32"));
idColumn.AutoIncrement=true;
DataColumn itemColumn = new DataColumn("Product", Type.GetType("System.Int32"));
DataColumn[] keyColumn= new DataColumn[1];
DataRow row;
DataSet changeDataSet;
table.Columns.Add(idColumn);
table.Columns.Add(itemColumn);
dataSet.Tables.Add(table);
keyColumn[0]= idColumn;
table.PrimaryKey=keyColumn;
for(int i = 0; i <10;i++)
{
row=table.NewRow();
row["Product"]= i;
table.Rows.Add(row);
}