我遇到了从DataTable.search获取结果的挑战。我正在使用的表达式没有得到结果。表达式的长度是否有限制?那可以用吗?使用DataTable中的所有列是错误的吗?
我正在尝试比较两个具有相同定义的DataTable中的行。使用第一个,我尝试获取不在第二个中的行并将它们保存在' ResultDataTable'以便使用保存值更新第一个DataTable的DataBase。
private DataTable ExtractRecord(DataTable localTable, DataTable importedTable)
{
DataTable returnTable = new DataTable(importedTable.TableName);
DataRow[] returnedDataRows;
Dictionary<int, string> DicColumnDataType = new Dictionary<int, string>();
for (int i = 0; i < importedTable.Columns.Count; i++)
{
DicColumnDataType.Add(i, importedTable.Columns[i].DataType.Name);
myDataColumn newCol = new myDataColumn(importedTable.Columns[i].ColumnName.ToString(), importedTable.Columns[i].DataType);
returnTable.Columns.Add(newCol);
returnTable.AcceptChanges();
}
foreach (DataRow row in importedTable.Rows)
{
object[] RowValues = row.ItemArray;
string Expression = "WHERE " + @" ";
int ColCount = localTable.Columns.Count;
for (int i = 0; i < ColCount; i++)
{
switch (DicColumnDataType[i])
{
case "Int32":
Expression = Expression.Remove(Expression.Length - 1, 1);
Expression += importedTable.Columns[i].ColumnName + " Like '%" + RowValues[i] + "%' AND " + " ";
break;
case "String":
Expression = Expression.Remove(Expression.Length - 1, 1);
Expression += importedTable.Columns[i].ColumnName + " Like '%" + RowValues[i] + "%' AND " + " ";
break;
case "Boolean":
Expression = Expression.Remove(Expression.Length - 1, 1);
Expression += importedTable.Columns[i].ColumnName + " Like '% " + RowValues[i] + "%' AND " + " ";
break;
case "DateTime":
Expression = Expression.Remove(Expression.Length - 1, 1);
Expression += importedTable.Columns[i].ColumnName + " Like '%#" + RowValues[i] + "#%' AND " + " ";
break;
}
}
Expression = Expression.Substring(0, Expression.Length - 6);
try
{
returnedDataRows = localTable.Select(Expression);
if (1 > returnedDataRows.Length)
{
returnTable.ImportRow(row);
returnedDataRows = null;
}
}
catch
{
}
}
returnTable.AcceptChanges();
return returnTable;
}