How to append one DataTable to another DataTable

后端 未结 6 1077
臣服心动
臣服心动 2020-12-10 09:59

I would like to append one DataTable to another DataTable. I see the DataTable class has two methods; \"Load(IDataReader)\" and \"Merge(DataTable)\". From the documentation

6条回答
  •  不知归路
    2020-12-10 10:31

    Add two datasets containing datatables, now it will merge as required

    DataSet ds1 = new DataSet();
    DataSet ds2 = new DataSet();
    
    DataTable dt1 = new DataTable();
    dt1.Columns.Add(new DataColumn("Column1", typeof(System.String)));
    
    DataRow newSelRow1 = dt1.NewRow();
    newSelRow1["Column1"] = "Select";
    dt1.Rows.Add(newSelRow1);
    
    DataTable dt2 = new DataTable();
    dt2.Columns.Add(new DataColumn("Column1", typeof(System.String)));
    
    DataRow newSelRow2 = dt1.NewRow();
    newSelRow2["Column1"] = "DataRow1Data";  // Data
    dt2.Rows.Add(newSelRow2);
    
    ds1.Tables.Add(dt1);
    ds2.Tables.Add(dt2);
    
    ds1.Tables[0].Merge(ds2.Tables[0]);
    

    Now ds1 will have the merged data

提交回复
热议问题