as i explain in the title, i cant set the property to the column:
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;
i am getting an error, that the object in not found after executing the following line:
tableCustomer.Columns["Customer_ID"].Properties["AutoIncrement"].Value = true;
the error is that the object is not found. but in all solutions in vb, they solve it like i wrote. what am i doing wrong?
You should add the "Customer_ID"
column like you did in the commented lines of your code. According to this source , you missed to set the ParentCatalog
property for the Column
.
You will need to set the ParentCatalog
for the column you wish to set properties on:
tableCustomer.Keys.Append("PrimaryKEy", KeyTypeEnum.adKeyPrimary, "Customer_ID");
tableCustomer.Columns["Customer_ID"].ParentCatalog = cat;
tableCustomer.Columns["Customer_ID"].Properties["AutoIncrement"].Value = true;
your answers are both right. but just, the catalog must be created before. here is the modified version:
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.