0

2 つのテーブルを持つデータベースを作成しようとしています。それらの1つに外部キーを追加したい。しかし、次のコードは機能していません。デバッグしたところ、唯一の問題は外部キーの追加にあることがわかりました。

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;
    }

次のような別のアプローチを試してみると(上の方法でデータベースを作成した後にデータベースを開く)、それも機能しません。

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 api の適切なドキュメントが見つからないため、これを解決する方法がわかりません。事前にt​​hnx

4

1 に答える 1