Primary Key finden ?

Wie kann ich von meinem VB-Programm aus
herausfinden welche Felder einer Tabelle in SQL-Server 7 Primary Keys sind ?

Wie kann ich von meinem VB-Programm aus
herausfinden welche Felder einer Tabelle
in SQL-Server 7 Primary Keys sind ?

Über ADOX: Tables-Auflistung, darin die Keys- bzw. Indexes-Auflistung…

Reinhard

Erhalte da aber beim Versuch auf die Keys zuzugreifen die Fehlermeldung:
„…angeforderte Operation wird von dem Provider nicht unterstützt“

[Bei dieser Antwort wurde das Vollzitat nachträglich automatisiert entfernt]

Kannst du mal deinen Code posten?

Reinhard

Kannst du mal deinen Code posten?

Reinhard

Private Sub btnGetKeyTest_Click()

Dim DBConn As New ADODB.Connection
Dim cat As New ADOX.Catalog
Dim DataKey As New ADOX.Key

DBConn.ConnectionString= „Provider=MSDASQL; Data Source= LSK;“
'Connection erfolgt über ODBC-Datenquellen-Definition

DBConn.Open
cat.ActiveConnection = DBConn

For Each DataKey In cat.Tables(„LSK“).Keys
If DataKey.Type = adKeyPrimary Then
MsgBox DataKey.Name & " is Primary Key"
End If
Next DataKey
DBConn.Close

End Sub

Über ODBC dürfte das wohl auch nicht gehen, versuch’ mal SQLOLEDB als Provider, etwa so:

Public Function TestADOX()
Dim DBConn As New ADODB.Connection
Dim Cat As New ADOX.Catalog
Dim Tbl As ADOX.Table
Dim DataKey As ADOX.Key

 DBConn.Open "Provider=sqloledb;" & \_
 "Data Source=MeinServer;Initial Catalog=pubs;User Id=sa:stuck\_out\_tongue\_winking\_eye:assword=; "
 Set Cat.ActiveConnection = DBConn
 Set Tbl = Cat.Tables("titles")
 For Each DataKey In Tbl.keys
 If DataKey.Type = adKeyPrimary Then
 MsgBox DataKey.Name & " is Primary Key"
 End If
 Next DataKey
 DBConn.Close
 Set Cat = Nothing
End Function

Reinhard

Oder alternativ:

Public Function TestADOX1()
Dim Cat As New ADOX.Catalog
Dim DataKey As ADOX.Key
 Cat.ActiveConnection = "Provider=sqloledb;" & \_
 "Data Source=MeinServer;Initial Catalog=pubs;User Id=sa:stuck\_out\_tongue\_winking\_eye:assword=; "
 For Each DataKey In Cat.Tables("titles").keys
 If DataKey.Type = adKeyPrimary Then MsgBox DataKey.Name & " is Primary Key"
 Next DataKey
 Set Cat = Nothing
End Function

Reinhard

Danke für den Tip!
Aber leider ist die Key’s Auflistung für die Tabelle leer ?!
Hab aber ne alternative gefunden:
hab ne stored Procedure gefunden, die lautet: sp_pkeys(‚Tabellenname‘);
die liefert einen Recordset mit den Primärschlüsseln der Tabelle zurück.
Danke für die Bemühungen!