Hallo,
ich habe noch nicht groß mit Datenbanken gearbeitet, wollte mich aber jetzt einarbeiten. Zur Übung wollte ich eine Datenbank mit zwei Tabellen anlegen, wobei eine Bücher verwaltet und die andere Autoren. Die Bücher haben dann ein Feld, welches auf einen Autor in der anderen Tabelle verweist. Leider gibts gerade bei dieser Verknüpfung eine Exception (unbeteiligte Spalten wurden herausgenommen):
Zitat: |
Unbehandelte Ausnahme: System.Runtime.InteropServices.COMException (0x800A0BB9):
Ausnahme von HRESULT: 0x800A0BB9
bei ADOX.Keys.Append(Object Item, KeyTypeEnum Type, Object Column, String Rel
atedTable, String RelatedColumn)
bei DatenbankTest_CS.Program.Tabellen_anlegen(String Name) in D:\Meine Visual
Projekte\DatenbankTest_CS\DatenbankTest_CS\Program.cs:Zeile 123.
bei DatenbankTest_CS.Program.Main(String[] args) in D:\Meine Visual Projekte\
DatenbankTest_CS\DatenbankTest_CS\Program.cs:Zeile 133.
|
Der Code sieht wie folgt aus:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66:
| ADOX.Column column;
ADOX.Catalog catalog = new ADOX.Catalog(); catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=E:\\NewMDB.mdb;");
ADOX.Table Buecher = new ADOX.Table(); Buecher.Name = "Books"; column = new ADOX.Column(); column.ParentCatalog = catalog; column.Name = "ID"; column.Type = ADOX.DataTypeEnum.adInteger; column.Properties["Nullable"].Value = false; column.Properties["AutoIncrement"].Value = true; Buecher.Columns.Append(column, ADOX.DataTypeEnum.adIUnknown, 0);
column = new ADOX.Column(); column.ParentCatalog = catalog; column.Name = "AuthorID"; column.Type = ADOX.DataTypeEnum.adInteger; column.Properties["Nullable"].Value = false; Buecher.Columns.Append(column, ADOX.DataTypeEnum.adIUnknown, 0); Buecher.ParentCatalog = catalog;
ADOX.Index index = new ADOX.Index(); index.Name = "PrimaryKey"; index.PrimaryKey = true; index.Columns.Append("ID", Buecher.Columns["ID"].Type, Buecher.Columns["ID"].DefinedSize); Buecher.Indexes.Append(index, null); ADOX.Table Autoren = new ADOX.Table(); Autoren.Name = "Authors"; column = new ADOX.Column(); column.ParentCatalog = catalog; column.Name = "ID"; column.Type = ADOX.DataTypeEnum.adInteger; column.Properties["Nullable"].Value = false; column.Properties["AutoIncrement"].Value = true; Autoren.Columns.Append(column, ADOX.DataTypeEnum.adIUnknown, 0); Autoren.ParentCatalog = catalog; index = new ADOX.Index(); index.Name = "PrimaryKey"; index.PrimaryKey = true; index.Columns.Append("ID", Buecher.Columns["ID"].Type, Buecher.Columns["ID"].DefinedSize); Autoren.Indexes.Append(index, null); ADOX.Key key = new ADOX.Key(); key.Name = "BuchAutor"; key.Type = ADOX.KeyTypeEnum.adKeyForeign; key.RelatedTable = "Authors"; key.Columns.Append("AuthorID", Buecher.Columns["AuthorID"].Type, Buecher.Columns["AuthorID"].DefinedSize); key.Columns["AuthorID"].RelatedColumn = "ID"; key.DeleteRule = ADOX.RuleEnum.adRICascade; Buecher.Keys.Append(key, ADOX.KeyTypeEnum.adKeyForeign, null, null, null);
catalog.Tables.Append(Autoren); catalog.Tables.Append(Buecher); |
Wobei beim Debuggen der Fehler bei Buecher.Keys.Append(key, ADOX.KeyTypeEnum.adKeyForeign, null/*Type.Missing*/, null, null); auftrat. Mir ist allerdings nicht klar wo bei ihm jetzt genau das Problem liegt, der Exceptiontext hilft mir auch nicht weiter.
Wisst ihr vielleicht wo der Fehler liegt - das wäre echt nett.
Vielen Dank schonmal im Vorraus,
viele Grüße
Andreas