问题
I want to check in an Access Database if a primary key exists using VB.Net & OleDb:
By primary key name
By number of fields as primary keys
回答1:
From here:
Public Shared Function getKeyNames(tableName As [String], conn As DbConnection) As List(Of String)
Dim returnList = New List(Of String)()
Dim mySchema As DataTable = TryCast(conn, OleDbConnection).GetOleDbSchemaTable(OleDbSchemaGuid.Primary_Keys, New [Object]() {Nothing, Nothing, tableName})
' following is a lengthy form of the number '3' :-)
Dim columnOrdinalForName As Integer = mySchema.Columns("COLUMN_NAME").Ordinal
For Each r As DataRow In mySchema.Rows
returnList.Add(r.ItemArray(columnOrdinalForName).ToString())
Next
Return returnList
End Function
来源:https://stackoverflow.com/questions/5065086/vb-net-how-can-i-check-if-a-primary-key-exists-in-an-access-db