如何在 adox 中设置 table 列的 AutoIncrement-property?
how to set the AutoIncrement-property of table column in 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;
我收到一个错误,执行以下行后未找到 object:
tableCustomer.Columns["Customer_ID"].Properties["AutoIncrement"].Value = true;
错误是找不到 object。
但是在 vb 中的所有解决方案中,他们都像我写的那样解决了它。
我做错了什么?
您应该像在代码的注释行中那样添加 "Customer_ID"
列。根据 this source,您错过了为 Column
.
设置 ParentCatalog
属性
您需要为要设置属性的列设置 ParentCatalog
:
tableCustomer.Keys.Append("PrimaryKEy", KeyTypeEnum.adKeyPrimary, "Customer_ID");
tableCustomer.Columns["Customer_ID"].ParentCatalog = cat;
tableCustomer.Columns["Customer_ID"].Properties["AutoIncrement"].Value = true;
你的回答都对。
但只是,必须先创建目录。
这是修改后的版本:
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;
}
正如我在标题中所解释的,我无法将 属性 设置为列:
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;
我收到一个错误,执行以下行后未找到 object:
tableCustomer.Columns["Customer_ID"].Properties["AutoIncrement"].Value = true;
错误是找不到 object。 但是在 vb 中的所有解决方案中,他们都像我写的那样解决了它。 我做错了什么?
您应该像在代码的注释行中那样添加 "Customer_ID"
列。根据 this source,您错过了为 Column
.
ParentCatalog
属性
您需要为要设置属性的列设置 ParentCatalog
:
tableCustomer.Keys.Append("PrimaryKEy", KeyTypeEnum.adKeyPrimary, "Customer_ID");
tableCustomer.Columns["Customer_ID"].ParentCatalog = cat;
tableCustomer.Columns["Customer_ID"].Properties["AutoIncrement"].Value = true;
你的回答都对。 但只是,必须先创建目录。 这是修改后的版本:
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;
}