VB.Net - 如何检查Access DB中是否存在主键

时间:2011-02-21 10:55:42

标签: vb.net ms-access primary-key constraints

如果使用VB.Net&存在主键,我想检查Access数据库。 OLEDB:

  • 按主键名称

  • 按字段数为主键

1 个答案:

答案 0 :(得分:3)

来自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