如何在adox中设置表格列的AutoIncrement属性?

时间:2015-05-21 15:38:44

标签: c# ms-access adox

正如我在标题中解释的那样,我无法将属性设置为列:

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

        try
        {                
            //Create the table Customer and it's fields. 
            tableCustomer.Name = "Customer";
            tableCustomer.Columns.Append("Customer_ID", ADOX.DataTypeEnum.adInteger);
            //column.ParentCatalog = cat;
            //column.Name = "Customer_ID";
            //column.Type = ADOX.DataTypeEnum.adInteger;
            //column.Properties["AutoIncrement"].Value = true;

            //tableCustomer.Columns.Append(column);

            tableCustomer.Keys.Append("PrimaryKEy", KeyTypeEnum.adKeyPrimary, "Customer_ID");
            tableCustomer.Columns["Customer_ID"].Properties["AutoIncrement"].Value = true;
            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("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", "Customer", "Customer_ID");
            tableAddresses.Columns.Append("Street", ADOX.DataTypeEnum.adVarWChar, 50);
            tableAddresses.Columns.Append("PostalCode", ADOX.DataTypeEnum.adVarWChar, 10);
            tableAddresses.Columns.Append("City", ADOX.DataTypeEnum.adVarWChar, 50);

            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.mdb");
            }                 
        }
        cat = null;

我收到错误,执行以下行后找不到该对象:

tableCustomer.Columns["Customer_ID"].Properties["AutoIncrement"].Value = true;

错误是找不到该对象。 但在vb的所有解决方案中,他们就像我写的一样解决它。 我做错了什么?

3 个答案:

答案 0 :(得分:1)

您应该像在代码的注释行中一样添加"Customer_ID"列。根据{{​​3}},您错过了为ParentCatalog设置Column属性。

答案 1 :(得分:1)

您需要为要设置属性的列设置ParentCatalog

tableCustomer.Keys.Append("PrimaryKEy", KeyTypeEnum.adKeyPrimary, "Customer_ID");
tableCustomer.Columns["Customer_ID"].ParentCatalog = cat;
tableCustomer.Columns["Customer_ID"].Properties["AutoIncrement"].Value = true;

答案 2 :(得分:0)

你的答案都是正确的。 但只是,必须先创建目录。 这是修改后的版本:

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

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

        ADODB.Connection con = new Connection();
        string connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source="
            + Application.StartupPath + "\\Customers.mdb";

        try
        {
            con.Open(connString);
            cat.ActiveConnection = con;

            //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["Customer_ID"].ParentCatalog = cat;
            tableCustomer.Columns["Customer_ID"].Properties["AutoIncrement"].Value = true;
            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("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["Address_ID"].ParentCatalog = cat;
            tableAddresses.Columns["Address_ID"].Properties["AutoIncrement"].Value = true;
            tableAddresses.Columns.Append("Customer_ID", ADOX.DataTypeEnum.adInteger);
            tableAddresses.Keys.Append("ForeignKey", KeyTypeEnum.adKeyForeign, "Customer_ID", "Customer", "Customer_ID");
            tableAddresses.Columns.Append("Street", ADOX.DataTypeEnum.adVarWChar, 50);
            tableAddresses.Columns.Append("PostalCode", ADOX.DataTypeEnum.adVarWChar, 10);
            tableAddresses.Columns.Append("City", ADOX.DataTypeEnum.adVarWChar, 50);

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