Solved

VB.net Check if a column exists in a SQL Table

Posted on 2014-11-11
5
507 Views
Last Modified: 2014-11-11
Hi
What VB.net code would I use to check if a column exists in a SQL table?
Thanks
0
Comment
Question by:murbro
5 Comments
 
LVL 2

Expert Comment

by:Pratik Makwana
ID: 40434702
This function is check if column is exist or not.....

Dim oField ' As ADODB.Field
      Dim oRecordset ' As ADODB.Recordset
      Dim oConn ' As ADODB.Connection
      Dim nameToCheck
      Dim nameExists
      
      ' The column name you're looking for
      nameToCheck = "ID"
      nameExists = false
      
      ' Create connection
      Set oConn = Server.CreateObject("ADODB.Connection")
      oConn.Open "YourConnectionString"
      
      Set oRecordset = oConn.Execute("SELECT * FROM YourTable")
      
      For Each oField In oRecordset.Fields
            If oField.Name = nameToCheck Then
                  nameExists = True
                  Exit For
            End If
      Next
      
      oRecordset.Close()
      oConn.Close()
      Set oConn = Nothing
      Set oRecordset = Nothing
      
      Response.Write("Field " & nameToCheck & " found: " & nameExists)
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 40434836
It is better not to use ADODB in a .Net application!

Do you already have the structure in a DataSet/DataTable?

If you want to go pure SQL, you could use this:
SELECT T.name AS TableName, C.name AS ColumnName 
FROM sys.tables AS T
INNER JOIN sys.columns AS C
ON C.object_id = T.object_id
WHERE T.name = 'RefPrefix'
AND C.name = 'Code2'

Open in new window

0
 
LVL 17

Expert Comment

by:OriNetworks
ID: 40435049
I think Eric has the best answer here where t.name is the table name you are checking and C.name is the column name you are checking for. If the column exists, it will return one row. If it does not exist, no rows will be returned.
0
 
LVL 27

Expert Comment

by:Ark
ID: 40436608
    Public Shared Function ColumnExists(ByVal tableName As String,
                                        ByVal columnName As String,
                                        ByVal conn As SqlClient.SqlConnection) As Boolean
        Dim strSQL As String = "SELECT COUNT(*) FROM information_schema.columns " & _
                               "WHERE table_schema = 'dbo' " & _
                               "AND table_name = '" & tableName & "'" & _
                               "AND column_name = '" & columnName & "';"
        Using cmd As New SqlClient.SqlCommand(strSQL, conn)
            Return CBool(cmd.ExecuteScalar)
        End Using
    End Function

Open in new window

Dim bExists As Boolean
Using conn As New SqlClent.SqlConnection(YourConnectionStringHere)
    conn.Open()
    bExists = ColumnExists(YourTableName, YourColumnName, conn)
    conn.Close()
End Using

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 40436653
Thanks
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now