private DataTable MergeSchema(DataTable table)
{
DataTable targetTable = null;
if (!_isStandAlonetable)
{
if (_dataSet.Tables.Contains(table.TableName, true))
{
if (_IgnoreNSforTableLookup)
{
targetTable = _dataSet.Tables[table.TableName];
}
else
{
targetTable = _dataSet.Tables[table.TableName, table.Namespace];
}
}
}
else
{
targetTable = _dataTable;
}
if (targetTable == null)
{
// in case of standalone table, we make sure that targetTable is not null, so if this check passes, it will be when it is called via detaset
if (MissingSchemaAction.Add == _missingSchemaAction)
{
targetTable = table.Clone(table.DataSet); // if we are here mainly we are called from DataSet.Merge at this point we don't set
//expression columns, since it might have refer to other columns via relation, so it wont find the table and we get exception;
// do it after adding relations.
_dataSet.Tables.Add(targetTable);
}
else if (MissingSchemaAction.Error == _missingSchemaAction)
{
throw ExceptionBuilder.MergeMissingDefinition(table.TableName);
}
}
else
{
if (MissingSchemaAction.Ignore != _missingSchemaAction)
{
// Do the columns
int oldCount = targetTable.Columns.Count;
for (int i = 0; i < table.Columns.Count; i++)
{
DataColumn src = table.Columns[i];
DataColumn dest = (targetTable.Columns.Contains(src.ColumnName, true)) ? targetTable.Columns[src.ColumnName] : null;
if (dest == null)
{
if (MissingSchemaAction.Add == _missingSchemaAction)
{
dest = src.Clone();
targetTable.Columns.Add(dest);
}
else
{
if (!_isStandAlonetable)
{
_dataSet.RaiseMergeFailed(targetTable, SR.Format(SR.DataMerge_MissingColumnDefinition, table.TableName, src.ColumnName), _missingSchemaAction);
}
else
{
throw ExceptionBuilder.MergeFailed(SR.Format(SR.DataMerge_MissingColumnDefinition, table.TableName, src.ColumnName));
}
}
}
else
{
if (dest.DataType != src.DataType ||
((dest.DataType == typeof(DateTime)) && (dest.DateTimeMode != src.DateTimeMode) && ((dest.DateTimeMode & src.DateTimeMode) != DataSetDateTime.Unspecified)))
{
if (!_isStandAlonetable)
_dataSet.RaiseMergeFailed(targetTable, SR.Format(SR.DataMerge_DataTypeMismatch, src.ColumnName), MissingSchemaAction.Error);
else
throw ExceptionBuilder.MergeFailed(SR.Format(SR.DataMerge_DataTypeMismatch, src.ColumnName));
}
MergeExtendedProperties(src.ExtendedProperties, dest.ExtendedProperties);
}
}
// Set DataExpression
if (_isStandAlonetable)
{
for (int i = oldCount; i < targetTable.Columns.Count; i++)
{
targetTable.Columns[i].Expression = table.Columns[targetTable.Columns[i].ColumnName].Expression;
}
}
// check the PrimaryKey
DataColumn[] targetPKey = targetTable.PrimaryKey;
DataColumn[] tablePKey = table.PrimaryKey;
if (targetPKey.Length != tablePKey.Length)
{
// special case when the target table does not have the PrimaryKey
if (targetPKey.Length == 0)
{
DataColumn[] key = new DataColumn[tablePKey.Length];
for (int i = 0; i < tablePKey.Length; i++)
{
key[i] = targetTable.Columns[tablePKey[i].ColumnName];
}
targetTable.PrimaryKey = key;
}
else if (tablePKey.Length != 0)
{
_dataSet.RaiseMergeFailed(targetTable, SR.DataMerge_PrimaryKeyMismatch, _missingSchemaAction);
}
}
else
{
for (int i = 0; i < targetPKey.Length; i++)
{
if (string.Compare(targetPKey[i].ColumnName, tablePKey[i].ColumnName, false, targetTable.Locale) != 0)
{
_dataSet.RaiseMergeFailed(table,
SR.Format(SR.DataMerge_PrimaryKeyColumnsMismatch, targetPKey[i].ColumnName, tablePKey[i].ColumnName),
_missingSchemaAction);
}
}
}
}
MergeExtendedProperties(table.ExtendedProperties, targetTable.ExtendedProperties);
}
return targetTable;
}