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;
}