简体   繁体   English

如何以编程方式复制具有复合键的MS Access表架构?

[英]How do I programmatically copy MS Access table schema which has composite key?

Let's say source MS Access database has a table called MyTable1. 假设源MS Access数据库有一个名为MyTable1的表。 And let's say MyTable1 has a composite primary key (Combination of two separate field Phone and City). 假设MyTable1有一个复合主键(两个单独的字段Phone和City的组合)。 Now when I copy using the following code, it does not make City as part of composite key in target table. 现在,当我使用以下代码复制时,它不会使City成为目标表中复合键的一部分。 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);

您应该遍历results []并将每个字段添加到idx.Columns集合中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建具有复合键的表,其中每个成员都是其他表的外键成员 - How do I create a table that has a composite key each member of which is a foreign key member to other tables 如何以编程方式在C#中创建MS Access表? - How do you create an MS Access table in C# programmatically? 如何让NHibernate返回复合键表上的数据? - How do I get NHibernate to return data on composite key table? 如何创建一个DbSyncForeignKeyConstraint到表具有复合主键 - how do I create a DbSyncForeignKeyConstraint to a table with a composite Primary Key 我有一个表,该表具有由三列组成的主键。 如何索引此表? - I have a table which has a primary key that is made of three columns. How do I index this table? 我如何播种具有外键的表,该表在模型中被指定为对象,但在表中被指定为GUID - How do i seed a table that has a foreign key, which is specified as an object in the model, but as a guid in the table 如何在 Entity Framework Core 中创建由基类和派生类组合而成的复合键 - How do I create composite key which is combination of base class and derived class in Entity Framework Core 如何制作一个由其中某个组合键排列的表? - How to make a table that has a composite key arranged by a certain one of them? 如何在MS CRM中更新地址组合? - How do I update address composite in MS CRM? 如何使用Entity Framework代码优先映射带有复合键的继承表? - How do I map an inherited table with a composite key using Entity Framework code-first?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM