首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用adox向access表添加外键

如何使用adox向access表添加外键
EN

Stack Overflow用户
提问于 2015-05-18 12:10:10
回答 1查看 1.1K关注 0票数 0

我正在尝试创建一个有两个表的数据库。我想在其中一个加一个外键。但是下面的代码不起作用,我调试它,发现唯一的问题是添加外键。

代码语言:javascript
复制
private static bool creatDatabase()
    {
        bool result = false;

        Catalog cat = new Catalog();
        Table tableCustomer = new Table();
        Table tableAddresses = new Table();

        //Create the table Customer and it's fields. 
        tableCustomer.Name = "Customer";
        tableCustomer.Columns.Append("Customer_ID", ADOX.DataTypeEnum.adInteger);
        tableCustomer.Keys.Append("PrimaryKEy", KeyTypeEnum.adKeyPrimary, "Customer_ID");
        tableCustomer.Columns.Append("Name", ADOX.DataTypeEnum.adVarWChar, 50);
        tableCustomer.Columns.Append("Email", ADOX.DataTypeEnum.adVarWChar, 50);
        tableCustomer.Columns.Append("TelNumber", ADOX.DataTypeEnum.adVarWChar, 32);
        tableCustomer.Columns.Append("Fax", ADOX.DataTypeEnum.adVarWChar, 32);
        tableCustomer.Columns.Append("Street", ADOX.DataTypeEnum.adVarWChar, 50);
        tableCustomer.Columns.Append("PostalCode", ADOX.DataTypeEnum.adInteger, 10);
        tableCustomer.Columns.Append("City", ADOX.DataTypeEnum.adVarWChar, 50);
        tableCustomer.Columns.Append("AdressCounter", ADOX.DataTypeEnum.adSmallInt);

        tableAddresses.Name = "Addresses";
        tableAddresses.Columns.Append("Address_ID", ADOX.DataTypeEnum.adInteger);
        tableAddresses.Keys.Append("PrimaryKEy", KeyTypeEnum.adKeyPrimary, "Address_ID");
        //tableAddresses.Columns.Append("Customer_ID", ADOX.DataTypeEnum.adInteger);            
        //tableAddresses.Keys.Append("ForeignKey", KeyTypeEnum.adKeyForeign, "Customer_ID");  ---> here is the Exception
        tableAddresses.Columns.Append("Street", ADOX.DataTypeEnum.adVarWChar, 50);
        tableAddresses.Columns.Append("PostalCode", ADOX.DataTypeEnum.adInteger, 10);
        tableAddresses.Columns.Append("City", ADOX.DataTypeEnum.adVarWChar, 50);

        try
        {
            cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Application.StartupPath + "\\Customers.mdb" + "; Jet OLEDB:Engine Type=5");
            cat.Tables.Append(tableCustomer);
            cat.Tables.Append(tableAddresses);

            //Now Close the database
            ADODB.Connection con = cat.ActiveConnection as ADODB.Connection;
            if (con != null)
                con.Close();


            result = true;
        }
        catch (Exception ex)
        {
            result = false;
        }
        finally
        {
            if (!result)
            {
                ADODB.Connection con = cat.ActiveConnection as ADODB.Connection;
                if (con != null)
                    con.Close();
                File.Delete(Application.StartupPath + "\\Customers.accdb");
            }                  
        }
        cat = null;
        return result;
    }

如果像下面这样尝试antoher方法(在用上一种方法创建数据库之后打开数据库),它也不能工作:

代码语言:javascript
复制
private static bool addForeignKey()
    {
        bool retValue = true;

        ADODB.Connection con = new Connection();
        Key kyForeign = new Key();
        Catalog cat = new Catalog();


        kyForeign.Name = "test";
        kyForeign.Type = KeyTypeEnum.adKeyForeign;
        kyForeign.RelatedTable = "Customer";
        kyForeign.Columns.Append("CustomerID", ADOX.DataTypeEnum.adInteger);
        kyForeign.Columns["CustomerID"].RelatedColumn = "Customer_ID";
        try
        {   
            con.Open("Provider='Microsoft.JET.OLEDB.4.0';Data source ='"
               + Application.StartupPath + "\\Customers.mdb';");
            cat.ActiveConnection = con;
            cat.Tables["Addresses"].Keys.Append(kyForeign, KeyTypeEnum.adKeyForeign, ADOX.DataTypeEnum.adInteger); // here comes the Exception
        }
        catch
        {
            retValue = false;
        }
        finally
        {
            if(retValue)
            {
                if (con != null)
                    con.Close();
            }
        }
        return retValue;
    }

我找不到一个好的文档为adox的代码示例,这就是为什么我不知道如何解决这个问题?先期

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-18 12:52:26

问题是您没有填充RelatedTableRelatedColumn参数。将第一段代码中的注释行替换为:

代码语言:javascript
复制
        tableAddresses.Columns.Append("Customer_ID", ADOX.DataTypeEnum.adInteger); 
        tableAddresses.Keys.Append("ForeignKey", ADOX.KeyTypeEnum.adKeyForeign, "Customer_ID", "Customer", "Customer_ID");
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30302872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档