1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| private void MergeCustomers() { string sqlAllCustomers = "SELECT * FROM Customers" ; string cnStr =@ "Data Source=.;Initial Catalog=northwind;Integrated Security=True" ; SqlConnection sqlCon = new SqlConnection(cnStr); sqlCon.Open(); SqlCommand cmd = new SqlCommand(sqlAllCustomers, sqlCon); SqlDataAdapter adpt = new SqlDataAdapter(cmd); DataTable dtCust1 = new DataTable( "Customers" ); adpt.Fill(dtCust1); dtCust1.PrimaryKey = new DataColumn[] { dtCust1.Columns[ "CustomerID" ] }; DataTable dtCust2 = dtCust1.Clone(); DataRow row1 = dtCust2.NewRow(); row1[ "CustomerID" ] = "ABCD" ; row1[ "CompanyName" ] = "GOFASHION" ; dtCust2.Rows.Add(row1); DataRow row2 = dtCust2.NewRow(); row2[ "CustomerID" ] = "DEFG" ; row2[ "CompanyName" ] = "HOANG NGUYEN" ; dtCust2.Rows.Add(row2); DataSet ds = new DataSet( "MyDataSet" ); ds.Tables.Add(dtCust1); ds.Merge(dtCust2); gvCustomer.DataSource = dtCust1; gvCustomer.DataBind(); } |
1
2
3
4
5
6
7
8
| string sqlAllCustomers = "SELECT * FROM Customers" ; string cnStr =@ "Data Source=.;Initial Catalog=northwind;Integrated Security=True" ; SqlConnection sqlCon = new SqlConnection(cnStr); sqlCon.Open(); SqlCommand cmd = new SqlCommand(sqlAllCustomers, sqlCon); SqlDataAdapter adpt = new SqlDataAdapter(cmd); DataTable dtCust1 = new DataTable( "Customers" ); adpt.Fill(dtCust1); |
1
2
3
4
5
6
7
8
| DataTable dtCust2 = dtCust1.Clone(); row1[ "CustomerID" ] = "ABCD" ; row1[ "CompanyName" ] = "GOFASHION" ; dtCust2.Rows.Add(row1); DataRow row2 = dtCust2.NewRow(); row2[ "CustomerID" ] = "DEFG" ; row2[ "CompanyName" ] = "HOANG NGUYEN" ; dtCust2.Rows.Add(row2); |
1
| DataTable dtCust2 = dtCust1.Clone(); |
1
| DataRow row1 = dtCust2.NewRow(); |
1
2
3
| row1[ "CustomerID" ] = "ABCD" ; row1[ "CompanyName" ] = "GOFASHION" ; dtCust2.Rows.Add(row1); |
1
2
3
4
| DataRow row2 = dtCust2.NewRow(); row2[ "CustomerID" ] = "DEFG" ; row2[ "CompanyName" ] = "HOANG NGUYEN" ; dtCust2.Rows.Add(row2); |
1
2
3
| DataSet ds = new DataSet( "MyDataSet" ); ds.Tables.Add(dtCust1); ds.Merge(dtCust2); |
Comments[ 0 ]
Đăng nhận xét