1

我有 2 个数据表

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)(数据应该是唯一的)。

有人请帮助我,并通过几个例子给我一些想法来解决我的问题。

4

2 回答 2

3

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

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

有关 DataTable.Merge() 的更多信息,请参阅MSDN 上的 DataTable.Merge 方法(DataTable)

于 2013-03-20T12:08:52.083 回答
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;
    }
于 2013-03-25T05:55:52.067 回答