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();
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM