Monday, June 11, 2007

Merging Data of the DataTables in the ADO.NET1.1

Here, I am giving 3 code samples for merging data from the datatables.

//common code

SqlConnection con=new SqlConnection(@"server=localhost\sqlexpress;database=demo;uid=sa;pwd=kkak");
con.Open();
SqlDataAdapter da1=new SqlDataAdapter("select * from table1",con);
SqlDataAdapter da2=new SqlDataAdapter("select * from table2",con);

// sample -1

DataSet ds1=new DataSet();
da1.Fill(ds1,"table1");
DataSet ds2=new DataSet();
da2.Fill(ds2,"table2");
ds1.Merge(ds2.Tables["table2"]);

// sample -2

DataSet ds1=new DataSet();
da1.Fill(ds1,"table1");
DataTable dt2=ds1.Tables["table1"].Clone();
da2.Fill(dt2);
ds1.Merge(dt2);

// sample -3

DataSet ds1=new DataSet();
da1.Fill(ds1,"table1");
DataSet ds2=new DataSet();
da2.Fill(ds2,"table2");
for(int i=0;i< ds2.Tables["table2"].Rows.Count;i++)
{
DataRow dr=ds2.Tables["table2"].Rows[i];
ds1.Tables["table1"].ImportRow(dr);
}

No comments: