I want to check in an Access Database if a primary key exists using VB.Net & OleDb:
By primary key name
开发者_如何学CBy number of fields as primary keys
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
精彩评论