I am using a DataSetHelper class from MS kb article 326009 HOW TO: Implement a DataSet SELECT INTO Helper Class in Visual C# .NET
Recetly I've noticed that InsertInto method throws exception if the source table doesn't have some columns from the target. It will be better to set columns to null or default.
The changed code is the following:
///
/// Sample of call
/// dsHelper.InsertInto(ds.Tables["TestTable"], ds.Tables["Employees"], "FirstName FName,LastName LName,BirthDate", "EmployeeID<5", "BirthDate") ;
///
public void InsertInto(DataTable DestTable, DataTable SourceTable,
string FieldList, string RowFilter, string Sort)
{
//
// This code copies the selected rows and columns from SourceTable and inserts them into DestTable.
//
ParseFieldList(FieldList, false);
DataRow[] Rows = SourceTable.Select(RowFilter, Sort);
DataRow DestRow;
foreach(DataRow SourceRow in Rows)
{
DestRow = DestTable.NewRow();
if (DataHelper.IsNullOrEmpty(FieldList))
{
foreach (DataColumn dc in DestRow.Table.Columns)
{
if (dc.Expression == "")
{
if (SourceTable.Columns.Contains(dc.ColumnName))//source can miss some target columns
DestRow[dc] = SourceRow[dc.ColumnName];
else
DebugHelper.LineWithTrace("The column is missing in the source:" + dc.ColumnName);
}
}
}
else
{
foreach(FieldInfo Field in m_FieldInfo)
{
DestRow[Field.FieldAlias] = SourceRow[Field.FieldName];
}
}
DestTable.Rows.Add(DestRow);
}
}
posted @ Monday, September 11, 2006 8:33 AM