Check if a table or field exists in a database

Function to check if a table or field exists in an MS Access or SQL Server database.

Introduction

These are two functions I wrote in VB.NET using ADO.NET to check and see if a table or a field exists in a database. It can work with MS Access, or SQL Server, or any other OLE database.

Background

Checking if a table or a field exists in an Access database should be a very simple task, but it can become very complicated with ADO.NET. With DAO or ADO in VB 6.0, this was an extremely easy task. Those who have used it will agree. So, I am posting these functions to help other programmers. Hopefully, it will help out some.

Using the code

Here are the functions:


''' <summary>
''' Checks to see if a table exists in Database or not.
''' </summary>
''' <param name="tblName">Table name to check</param>
''' <param name="cnnStr">Connection String to connect to</param>
''' <returns>Works with Access or SQL</returns>
''' <remarks></remarks>

Public Function DoesTableExist(ByVal tblName As String, ByVal cnnStr As String) As Boolean
    ' For Access Connection String,
    ' use "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
    ' accessFilePathAndName

    ' Open connection to the database
    Dim dbConn As New OleDbConnection(cnnStr)
    dbConn.Open()
    
    ' Specify restriction to get table definition schema
    ' For reference on GetSchema see:
    ' http://msdn2.microsoft.com/en-us/library/ms254934(VS.80).aspx

    Dim restrictions(3) As String
    restrictions(2) = tblName
    Dim dbTbl As DataTable = dbConn.GetSchema("Tables", restrictions)

    If dbTbl.Rows.Count = 0 Then
        'Table does not exist
        DoesTableExist = False
    Else
        'Table exists
        DoesTableExist = True
    End If

    dbTbl.Dispose()
    dbConn.Close()
    dbConn.Dispose()
End Function


''' <summary>
''' Checks to see if a field exists in table or not.
''' </summary>
''' <param name="tblName">Table name to check in</param>
''' <param name="fldName">Field name to check</param>
''' <param name="cnnStr">Connection String to connect to</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function DoesFieldExist(ByVal tblName As String, _
                               ByVal fldName As String, _
                               ByVal cnnStr As String) As Boolean
    ' For Access Connection String,
    ' use "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
    ' accessFilePathAndName

    ' Open connection to the database
    Dim dbConn As New OleDbConnection(cnnStr)
    dbConn.Open()
    Dim dbTbl As New DataTable

    ' Get the table definition loaded in a table adapter
    Dim strSql As String = "Select TOP 1 * from " & tblName
    Dim dbAdapater As New OleDbDataAdapter(strSql, dbConn)
    dbAdapater.Fill(dbTbl)

    ' Get the index of the field name
    Dim i As Integer = dbTbl.Columns.IndexOf(fldName)

    If i = -1 Then
        'Field is missing
        DoesFieldExist = False
    Else
        'Field is there
        DoesFieldExist = True
    End If

    dbTbl.Dispose()
    dbConn.Close()
    dbConn.Dispose()
End Function


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值