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

陌路散爱 提交于 2020-02-08 07:26:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!