开发者

VB.Net - How can I check if a primary key exists in an Access DB

开发者 https://www.devze.com 2023-02-12 02:56 出处:网络
I want to check in an Access Database if a primary key exists using VB.Net & OleDb: By primary key name

I want to check in an Access Database if a primary key exists using VB.Net & OleDb:


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
0

精彩评论

暂无评论...
验证码 换一张
取 消