Geeks With Blogs
Razi Syed Professional Developer

Here is a function I wrote to check if a table exists in database or not:

    ''' <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 reference on GetSchema see:
        '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)

        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
            'Table exists
            DoesTableExist = True
        End If

    End Function

Posted on Thursday, November 13, 2008 11:07 AM Visual Basic 2005 , SQL Server 2005 , MS Access | Back to top

Comments on this post: Function to check if a table exists in a database or not

# re: Function to check if a table exists in a database or not
Requesting Gravatar...
Its Return False If also
Table Is already exist but it haven't any row ..
Left by Ketan on Jan 05, 2009 4:16 AM

Your comment:
 (will show your gravatar)

Copyright © razisyed | Powered by: