0

Let’s say source MS Access database has a table called MyTable1. And let’s say MyTable1 has a composite primary key (Combination of two separate field Phone and City). Now when I copy using the following code, it does not make City as part of composite key in target table. How do fix

            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);
Shai
  • 529
  • 7
  • 20
  • 36

1 Answers1

0

You should iterate through results[] and add each field into idx.Columns collection

4dmonster
  • 3,012
  • 1
  • 14
  • 24