VB.Net - 如何检查主键是否存在于Access DB中 [英] VB.Net - How can I check if a primary key exists in an Access DB

查看:887
本文介绍了VB.Net - 如何检查主键是否存在于Access DB中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查一个访问数据库,如果主键存在使用VB.Net& OleDb:




  • 按主键名称


  • ://stackoverflow.com/questions/862749/how-to-get-the-primary-key-of-an-ms-access-table-in-c>这里:



    公共共享函数getKeyNames(tableName As [String],conn As DbConnection)As List(Of String)
    Dim returnList = New List String)()


    Dim mySchema As DataTable = TryCast(conn,OleDbConnection).GetOleDbSchemaTable(OleDbSchemaGuid.Primary_Keys,New [Object](){Nothing,Nothing,tableName})


    '以下是数字'3'的长度形式:-)
    Dim columnOrdinalForName As Integer = mySchema.Columns(COLUMN_NAME)。Ordinal

    每个r作为DataRow In mySchema.Rows
    returnList.Add(r.ItemArray(columnOrdinalForName).ToString())
    接下来

    返回returnList
    结束函数


    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

    解决方案

    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
    

    这篇关于VB.Net - 如何检查主键是否存在于Access DB中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆