Допустим, в исходной базе данных MS Access есть таблица с именем MyTable1. И допустим, MyTable1 имеет составной первичный ключ (комбинация двух отдельных полей Phone и City). Теперь, когда я копирую, используя следующий код, город не становится частью составного ключа в целевой таблице. Как исправить
ADOX.Table sourceTable = default(ADOX.Table);
sourceTable = sourceCat.Tables[tableName.Trim()];
ADOX.Table newTable = new ADOX.Table();
newTable.ParentCatalog = targetCat;
tempNewtableName = sourceCat.Tables[tableName.Trim()].Name;
newTable.Name = tempNewtableName;
ADOX.Column newCol = default(ADOX.Column);
DataTable primaryKeyDT = new DataTable();
primaryKeyDT.Columns.Add("FieldName");
primaryKeyDT.Columns.Add("Type");
foreach (ADOX.Index idx1 in sourceCat.Tables[tableName].Indexes)
{
if (idx1.PrimaryKey == true)
{
primaryKeyDT.Rows.Add(idx1.Columns[0].Name, idx1.Name);
}
}
foreach (ADOX.Column SourceCol in sourceTable.Columns)
{
newCol = new ADOX.Column();
newCol.Type = SourceCol.Type;
newCol.DefinedSize = SourceCol.DefinedSize;
newCol.ParentCatalog = targetCat;
newCol.Precision = SourceCol.Precision;
newCol.DefinedSize = SourceCol.DefinedSize;
newCol.Attributes = SourceCol.Attributes;
newCol.Name = SourceCol.Name;
newCol.NumericScale = SourceCol.NumericScale;
newTable.Columns.Append(newCol);
DataRow[] results = primaryKeyDT.Select("FieldName ='" + SourceCol.Name + "'");
if (results.Length > 0)
{
idx = new Index();
idx.Name = "idx_" + SourceCol.Name;
idx.PrimaryKey = true;
idx.Columns.Append(SourceCol.Name);
newTable.Indexes.Append(idx);
}
}
targetCat.Tables.Append(newTable);