我调用了这个方法,结果只显示了前四个表的访问文件,并且错误信息出现在try-catch的消息框中。在添加table6,table7和一些键之前,这个方法还可以。有什么解决方案吗?
public void createStockAccess()
{
ADOX.Catalog cat = new ADOX.Catalog();
ADOX.Table table = new ADOX.Table();
ADOX.Table table2 = new ADOX.Table();
ADOX.Table table3 = new ADOX.Table();
ADOX.Table table4 = new ADOX.Table();
ADOX.Table table5 = new ADOX.Table();
ADOX.Table table6 = new ADOX.Table();
ADOX.Table table7 = new ADOX.Table();
//Create the table and it's fields.
table.Name = "Gold";
table.Columns.Append("Weight", ADOX.DataTypeEnum.adDouble);
table.Columns.Append("DollarPerGram", ADOX.DataTypeEnum.adCurrency);
table2.Name = "Diamond";
table2.Columns.Append("Size", ADOX.DataTypeEnum.adVarWChar, 4);
table2.Columns.Append("Quantity", ADOX.DataTypeEnum.adInteger, 4);
table2.Columns.Append("Carat", ADOX.DataTypeEnum.adDouble);
table2.Columns.Append("DollarPerCarat", ADOX.DataTypeEnum.adCurrency);
table2.Keys.Append("DiamondPK", KeyTypeEnum.adKeyPrimary, "Size");
table3.Name = "Parts";
table3.Columns.Append("Number", ADOX.DataTypeEnum.adVarWChar, 10);
table3.Columns.Append("Weight", ADOX.DataTypeEnum.adDouble);
table3.Columns.Append("DollarPerGram", ADOX.DataTypeEnum.adCurrency);
table3.Keys.Append("PartsPK", KeyTypeEnum.adKeyPrimary, "Number");
table4.Name = "Jewel";
table4.Columns.Append("Number", ADOX.DataTypeEnum.adVarWChar, 9);
table4.Columns.Append("TypeNumber", ADOX.DataTypeEnum.adVarWChar, 10);
table4.Columns.Append("Manufacturer", ADOX.DataTypeEnum.adVarWChar, 30);
table4.Columns.Append("ManuCost", ADOX.DataTypeEnum.adCurrency);
table4.Columns.Append("Price", ADOX.DataTypeEnum.adCurrency);
table4.Columns.Append("GoldColor", ADOX.DataTypeEnum.adVarWChar, 10);
table4.Columns.Append("GoldCarat", ADOX.DataTypeEnum.adVarWChar, 3);
table4.Columns.Append("GoldWeight", ADOX.DataTypeEnum.adDouble);
table4.Columns.Append("Type", ADOX.DataTypeEnum.adVarWChar, 20);
table4.Columns.Append("Cost", ADOX.DataTypeEnum.adCurrency);
table4.Columns.Append("Status", ADOX.DataTypeEnum.adVarWChar, 15);
table4.Columns["Manufacturer"].Attributes = ColumnAttributesEnum.adColNullable;
table4.Columns["ManuCost"].Attributes = ColumnAttributesEnum.adColNullable;
table4.Columns["GoldCarat"].Attributes = ColumnAttributesEnum.adColNullable;
table4.Columns["GoldWeight"].Attributes = ColumnAttributesEnum.adColNullable;
table4.Columns["Cost"].Attributes = ColumnAttributesEnum.adColNullable;
table4.Keys.Append("PrimaryKey", KeyTypeEnum.adKeyPrimary, "Number", "JewelDiamond", "Number");
table5.Name = "JewelDiamond";
table5.Columns.Append("Number", ADOX.DataTypeEnum.adVarWChar, 9);
table5.Columns.Append("Size", ADOX.DataTypeEnum.adVarWChar, 4);
table5.Columns.Append("Quantity", ADOX.DataTypeEnum.adInteger, 4);
table5.Columns.Append("Carat", ADOX.DataTypeEnum.adDouble);
table5.Columns.Append("Type", ADOX.DataTypeEnum.adVarWChar, 20);
table5.Keys.Append("PrimaryKeyJD", KeyTypeEnum.adKeyPrimary, "Number");
table5.Keys["PrimaryKeyJD"].Columns.Append("Size", ADOX.DataTypeEnum.adVarWChar, 4);
table5.Keys.Append("ForeignKeyJD", KeyTypeEnum.adKeyForeign, "Number", "Jewel", "Number");
table6.Name = "Type";
table6.Columns.Append("TypeNumber", ADOX.DataTypeEnum.adVarWChar, 10);
table6.Columns.Append("Picture", ADOX.DataTypeEnum.adVarWChar, 150);
table6.Columns["Picture"].Attributes = ColumnAttributesEnum.adColNullable;
table6.Keys.Append("typePK", KeyTypeEnum.adKeyPrimary, "TypeNumber");
table7.Name = "TypeInfo";
table7.Columns.Append("TypeNumber", ADOX.DataTypeEnum.adVarWChar, 10);
table7.Columns.Append("Size", ADOX.DataTypeEnum.adVarWChar, 4);
table7.Columns.Append("Quantity", ADOX.DataTypeEnum.adDouble);
table7.Keys.Append("infoPK", KeyTypeEnum.adKeyPrimary, "TypeNumber");
table7.Keys["infoPK"].Columns.Append("Size", ADOX.DataTypeEnum.adVarWChar, 4);
table7.Keys.Append("infoFK", KeyTypeEnum.adKeyForeign, "TypeNumber", "Type", "TypeNumber");
table4.Keys.Append("ForeignKey", KeyTypeEnum.adKeyForeign, "TypeNumber", "Type", "TypeNumber");
try
{
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=Stock.mdb; Jet OLEDB:Engine Type=5");
cat.Tables.Append(table);
cat.Tables.Append(table2);
cat.Tables.Append(table3);
cat.Tables.Append(table4);
cat.Tables.Append(table5);
cat.Tables.Append(table6);
cat.Tables.Append(table7);
//Now Close the database
Interop.Adodb.Connection con = cat.ActiveConnection as Interop.Adodb.Connection;
if (con != null)
con.Close();
}
catch (Exception ex)
{
MessageBox.Show("cannot create stock database.");
}
cat = null;
}
答案 0 :(得分:0)
始终在错误捕获时提供异常消息和堆栈跟踪 - 这是问题解决方案的关键。
我猜,最可能的问题是以下代码行:
table4.Keys.Append("PrimaryKey", KeyTypeEnum.adKeyPrimary, "Number", "JewelDiamond", "Number");
添加主键并定义相关的表和列。也许,你复制代码时就错过了这个。
应该是:
table4.Keys.Append("PrimaryKey", KeyTypeEnum.adKeyPrimary, "Number");