将两个数据表与两个表上的空值合并 - C#

Combine two data tables with null values on both tables - C#

我有两个数据 tables 如下所示。

数据table1:

table1_id(PK)  DriverID    Vehicle    
   111          Ram00       VRN01       
   112          Shyam00     VRN02
   113          Ram00       VRN03

数据table2:

 table2_id(PK)  DriverID     exit_time 
   AA1          Ram00        10.10AM      
   AA2          Hari00       11.20PM
  

合并输出

table1_id     DriverID    Vehicle     table2_id   exit_time 
   111          Ram00       VRN01       AA1         10.10AM 
   112          Shyam00     VRN02       NULL        NULL
   113          Ram00       VRN03       AA1         10.10AM 
   NULL         Hari00       NULL       AA2         11:20PM

DriverID 在两者中都很常见 table。但是仅仅合并两个数据table 不会给出这个结果。 请帮助实现这一目标。

datatable1.Merge(datatable2);

您可以使用两个数据tables通过[=13]组合成一个数据table =]编码和删除额外的列之后For循环结束,检查我的代码它会工作。

string Qry = "select tab1.table_id,'' as DriverID,vehicle,tab1.driver_id Tab1DrvrID,exit_time from tab1 " +
              "full join tab2 on tab2.driver_id=tab1.driver_id";
            cmd = new SqlCommand(Qry, con);
            da = new SqlDataAdapter(cmd);
            dt = new DataTable();
            da.Fill(dt);

            //string DrvrID;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string Qry2 = "select tab1.table_id,'' as DriverID,vehicle,tab1.driver_id Tab1DrvrID,tab2.driver_id Tab2DrvrID,exit_time from tab1 " +
                   "full join tab2 on tab2.driver_id=tab1.driver_id ";
                cmd = new SqlCommand(Qry2, con);
                SqlDataAdapter daa = new SqlDataAdapter();
                DataTable dtt = new DataTable();
                daa = new SqlDataAdapter(cmd);
                daa.Fill(dtt);

                if (dtt.Rows.Count > 0)//
                {
                    string s=dtt.Rows[i]["Tab1DrvrID"].ToString();
                    if (s=="")
                    {
                        dt.Rows[i]["DriverID"] = dtt.Rows[i]["Tab2DrvrID"].ToString();
                    }
                    else
                    {
                        dt.Rows[i]["DriverID"] = dtt.Rows[i]["Tab1DrvrID"].ToString();
                    }
                }
                else
                {

                }
                dt.AcceptChanges();
            }
            dt.Columns.Remove("Tab1DrvrID");
如果来自两个 DATATABLES 的列在同一个 DATATYPE 中匹配,

Merge 可以正常工作。如果该列在 DATATABLE 的第一行中具有 NULL 值,则该列 DATATYPE 将为字符串。所以第二个 DATATABLE,如果有值 Date 或任何其他类型,将错过合并时的匹配。要解决此问题,您需要使两个 DATATABLES 列相同 DATATYPE.

    private DataTable MergeDataTable(DataTable dataTable1, DataTable dataTable2)
    {
        var dtCloned = dataTable2.Clone();

        foreach (DataColumn column in dataTable2.Columns)
        {
            var col = dataTable1.Columns[column.ColumnName];
            if (col != null && col.DataType != column.DataType )
            {
                dtCloned.Columns[column.ColumnName].DataType = col.DataType;
            }
        }
        foreach (DataRow d in dataTable2.Rows)
        {
            dtCloned.ImportRow(d);
        }

        dataTable1.Merge(dtCloned);

        return dataTable1;
    }