Hallo zusammen,
ich möchte zwei Tabellen in einer Access 2000 DB miteinander verknüpfen. Solange es sich dabei um eine einfache Master-Detail-Verbindung handelt, ist dies auch nicht weiter problematisch, d.h. es existiert zu einem Fremdschlüssel ein "echter" Primärschlüssel in der anderen Tabelle. Problematisch wird es bei zusammengesetzten Primärschlüsseln. Ich wollte das Problem mit Hilfe des MSDN lösen. Microsoft schlägt folgenden Lösungsansatz in Visual Basic vor:
"Creating One-to-Many Relationships (Foreign Keys)
One-to-many relationships between tables (where the primary key value in the primary table may appear in <B>multiple rows</B> [!] in the foreign table) are established by creating foreign keys. A foreign key is a column or combination of columns whose values match the primary key of another table. Unlike a primary key, a foreign key does not have to be unique."
ADOX
<PRE>
Sub ADOCreateForeignKey()
Dim cat As New ADOX.Catalog
Dim tbl As ADOX.Table
Dim fk As New ADOX.Key
' Open the catalog
cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=.\NorthWind.mdb;"
' Get the table for the foreign side of the relationship
Set tbl = cat.Tables("Products")
' Create the Foreign Key
fk.Name = "CategoriesProducts"
fk.Type = adKeyForeign
fk.RelatedTable = "Categories"
' Append column(s) in the foreign table to it
fk.Columns.Append "CategoryId"
' Set RelatedColumn property to the name of the corresponding
' column in the primary table
fk.Columns("CategoryId").RelatedColumn = "CategoryId"
' Append the Key object to the Keys collection of Table
tbl.Keys.Append fk
Set cat = Nothing
End Sub
</PRE>
Wie kann ich die Zeile "tbl.Keys.Append fk" in Delphi 5 umsetzen? Wie müssen die restlichen Parameter des Delphi-Äquivalents "_Keys.Append()" gefüllt werden? Der folgende Code produziert nur Laufzeit-Fehlermeldungen:
<PRE>
(...)
var
ADOXKey: _Key;
ADOXKey := CoKey.Create;
try
ADOXKey.Name := 'Tabelle1Tabelle2';
ADOXKey.Type_ := adKeyForeign;
ADOXKey.RelatedTable := 'Tabelle1';
ADOXColumn := ADOXTable.Columns.Item['ID1'] as _Column;
ADOXKey.Columns.Append(ADOXColumn.Name, ADOXColumn.Type_, ADOXColumn.DefinedSize);
ADOXKey.Columns['ID1'].RelatedColumn := 'ID1';
//weitere Spalten sollen hier folgen
ADOXTable.Keys.Append(ADOXKey, adKeyForeign, EmptyParam, '', ''); //???
finally
ADOXKey := nil;
end;
</PRE>
Was ist an dieser Umsetzung des Microsoft-Beispiels falsch? Bin für jeden Hinweis dankbar!
Gruss,
Andreas Kohl
ich möchte zwei Tabellen in einer Access 2000 DB miteinander verknüpfen. Solange es sich dabei um eine einfache Master-Detail-Verbindung handelt, ist dies auch nicht weiter problematisch, d.h. es existiert zu einem Fremdschlüssel ein "echter" Primärschlüssel in der anderen Tabelle. Problematisch wird es bei zusammengesetzten Primärschlüsseln. Ich wollte das Problem mit Hilfe des MSDN lösen. Microsoft schlägt folgenden Lösungsansatz in Visual Basic vor:
"Creating One-to-Many Relationships (Foreign Keys)
One-to-many relationships between tables (where the primary key value in the primary table may appear in <B>multiple rows</B> [!] in the foreign table) are established by creating foreign keys. A foreign key is a column or combination of columns whose values match the primary key of another table. Unlike a primary key, a foreign key does not have to be unique."
ADOX
<PRE>
Sub ADOCreateForeignKey()
Dim cat As New ADOX.Catalog
Dim tbl As ADOX.Table
Dim fk As New ADOX.Key
' Open the catalog
cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=.\NorthWind.mdb;"
' Get the table for the foreign side of the relationship
Set tbl = cat.Tables("Products")
' Create the Foreign Key
fk.Name = "CategoriesProducts"
fk.Type = adKeyForeign
fk.RelatedTable = "Categories"
' Append column(s) in the foreign table to it
fk.Columns.Append "CategoryId"
' Set RelatedColumn property to the name of the corresponding
' column in the primary table
fk.Columns("CategoryId").RelatedColumn = "CategoryId"
' Append the Key object to the Keys collection of Table
tbl.Keys.Append fk
Set cat = Nothing
End Sub
</PRE>
Wie kann ich die Zeile "tbl.Keys.Append fk" in Delphi 5 umsetzen? Wie müssen die restlichen Parameter des Delphi-Äquivalents "_Keys.Append()" gefüllt werden? Der folgende Code produziert nur Laufzeit-Fehlermeldungen:
<PRE>
(...)
var
ADOXKey: _Key;
ADOXKey := CoKey.Create;
try
ADOXKey.Name := 'Tabelle1Tabelle2';
ADOXKey.Type_ := adKeyForeign;
ADOXKey.RelatedTable := 'Tabelle1';
ADOXColumn := ADOXTable.Columns.Item['ID1'] as _Column;
ADOXKey.Columns.Append(ADOXColumn.Name, ADOXColumn.Type_, ADOXColumn.DefinedSize);
ADOXKey.Columns['ID1'].RelatedColumn := 'ID1';
//weitere Spalten sollen hier folgen
ADOXTable.Keys.Append(ADOXKey, adKeyForeign, EmptyParam, '', ''); //???
finally
ADOXKey := nil;
end;
</PRE>
Was ist an dieser Umsetzung des Microsoft-Beispiels falsch? Bin für jeden Hinweis dankbar!
Gruss,
Andreas Kohl
Comment