Click here to Skip to main content
6,295,667 members and growing! (14,160 online)
Email Password   helpLost your password?
Database » Database » ADO.NET     Beginner License: The Code Project Open License (CPOL)

Check if a table or field exists in a database

By R. Syed

Function to check if a table or field exists in an MS Access or SQL Server database.
VB, .NET, ADO.NET, Dev
Posted:13 Nov 2008
Views:9,636
Bookmarked:15 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
6 votes for this article.
Popularity: 2.48 Rating: 3.18 out of 5
1 vote, 16.7%
1

2
2 votes, 33.3%
3
3 votes, 50.0%
4

5

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

R. Syed


Member
I have been writing programs as a hobby since I was a teenager, and I have been working as a Professional Developer after graduating from University of Houston since 2002. Currently, I am one of the senior Developers at a Software Firm in Dallas, TX.

My area of expertise is developing management database systems for business so that they can efficiently store and lookup existing customer's information and related data, and be able to generate various reports. My goal is to always deliver innovative design and a user friendly interface.
Occupation: Software Developer (Senior)
Location: United States United States

Other popular Database articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
Generalgood article PinmemberDonsw14:38 7 Feb '09  
GeneralNice and simple PinmemberPetter Ivarsson9:49 24 Nov '08  
GeneralRe: Nice and simple PinmemberRazi Syed19:57 2 Dec '08  
GeneralDoes the getSchema need the user to have DBA permissions? Pinmemberanushyam7:36 17 Nov '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 13 Nov 2008
Editor: Smitha Vijayan
Copyright 2008 by R. Syed
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project