如何比较(行和列)两个DataTable并获取唯一记录

时间:2013-03-20 11:58:15

标签: c# datatable compare

我有2个DataTable

string query = "Select * from " + ServerTableName;
DataTable oDtSeverData = GetDataTable(query);

string dbQuery = "SELECT * from " + LocalSystemTableName;
DataTable oDtLocalSystemData = dataService.GetDataTable(dbQuery);

我想比较(行和列)数据表,以获得具有唯一记录(唯一行)的相同列(存在于数据表中)。

让我详细解释一下:

oDtServerData包含少量行的列(Column1,Column2,Column3,Column4)。 oDtLocalSystemData包含少量行的列(Column1,Column2,Column3)。

oDtServerData可能有更少的列和oDtLocalSystemData。但在任何情况下我都希望列(Column1,Column2,Column3)在数据表中与唯一行匹配(数据应该是唯一的)。

有人请帮助我,并给我一些想法来解决我的问题。

2 个答案:

答案 0 :(得分:2)

您可以使用以下代码比较两个DataTable,

public static DataTable CompareTwoDataTable(DataTable dt1, DataTable dt2)
{ 
  dt1.Merge(dt2);
  DataTable d3 = dt2.GetChanges();
  return d3;
}

有关DataTable.Merge()的详细信息,请参阅DataTable.Merge Method (DataTable) on MSDN

答案 1 :(得分:0)

ArrayList commonColumns = new ArrayList();

for (int iServerColumnCount = 0; iServerColumnCount < oDtSeverData .Columns.Count; iServerColumnCount ++)
{
  for (int iLocalColumnCount = 0;
                             iLocalColumnCount < oDtLocalSystemData .Columns.Count;
                             iLocalColumnCount ++)
    {
      if (oDtSeverData .Columns[iServerColumnCount ].ColumnName.ToString()
             .Equals(oDtLocalSystemData .Columns[iLocalColumnCount].ColumnName.ToString()))
      {
         commonColumns.Add(oDtLocalSystemData .Columns[iLocalColumnCount].ColumnName.ToString());
      }
    }
}

DataTable oDtData = CompareTwoDataTable(oDtLocalSystemData, oDtSeverData,commonColumns);

public DataTable CompareTwoDataTable(DataTable dtOriginalTable, DataTable dtNewTable, ArrayList columnNames)
    {
        DataTable filterTable = new DataTable();
        try
        {
            filterTable = dtNewTable.Copy();
            string filterCriterial;
            if (columnNames.Count > 0)
            {
                for (int iNewTableRowCount = 0; iNewTableRowCount < dtNewTable.Rows.Count; iNewTableRowCount++)
                {
                    filterCriterial = string.Empty;
                    foreach (string colName in columnNames.ToArray())
                    {

                        filterCriterial += "ISNULL("+colName.ToString() + ",'')='" + dtNewTable.Rows[iNewTableRowCount][colName].ToString() + "' AND ";
                    }
                    filterCriterial = filterCriterial.TrimEnd((" AND ").ToCharArray());
                    DataRow[] dr = dtOriginalTable.Select(filterCriterial);
                    if (dr.Length > 0)
                    {
                        filterTable.Rows[filterTable.Rows.IndexOf(filterTable.Select(filterCriterial)[0])].Delete();
                        filterTable.AcceptChanges();
                    }
                }
            }

        }
        catch (Exception ex)
        {
        }

        return filterTable;
    }

我试图将数据插入到表中,因为我使用了相同的公共列

,因此我使用了批量插入
 public bool BulkInsertDataTable(string tableName, DataTable dataTable, string[] commonColumns)
    {
        bool isSuccuss;
        try
        {

            SqlConnection SqlConnectionObj = GetSQLConnection();
            SqlBulkCopy bulkCopy = new SqlBulkCopy(SqlConnectionObj, SqlBulkCopyOptions.TableLock | SqlBulkCopyOptions.FireTriggers | SqlBulkCopyOptions.UseInternalTransaction, null);
            bulkCopy.DestinationTableName = tableName;
            bulkCopy.ColumnMappings.Clear();
            for (int iDtColumnCount = 0; iDtColumnCount < dataTable.Columns.Count; iDtColumnCount++)
            {
                for (int iArrCount = 0; iArrCount < commonColumns.Length; iArrCount++)
                {
                    if (dataTable.Columns[iDtColumnCount].ColumnName.ToString().Equals(commonColumns[iArrCount].ToString()))
                    {
                        bulkCopy.ColumnMappings.Add(dataTable.Columns[iDtColumnCount].ColumnName.ToString(),
                                                    commonColumns[iArrCount].ToString());
                    }
                }
            }

            bulkCopy.WriteToServer(dataTable);
            isSuccuss = true;
        }
        catch (Exception ex)
        {
            isSuccuss = false;
        }
        return isSuccuss;
    }