Solved

Check if SQL table exists from vb.net

Posted on 2014-02-23
2
757 Views
Last Modified: 2014-02-23
I need to check an SQl table exists from vb.net

I have seen lots of blogs but i can not get any of them to work in my code.

I have this which does not error, but i need to know what to add to do the check and drop the table if it exists

As SqlConnection = New SqlConnection(myserver details)
Dim cmd As SqlCommand
Dim con
 cmd = New SqlCommand("SELECT * FROM SYS.OBJECTS WHERE TYPE = 'u' AND NAME = 'temptbl_Stops'", con)
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()

Any help most welcome, i am a total novice as you have probably guessed
0
Comment
Question by:SweetingA
2 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 500 total points
Comment Utility
cmd = New SqlCommand("SELECT OBJECT_ID('temptbl_Stops')")

If the result is non-null than the table exists.
0
 

Author Closing Comment

by:SweetingA
Comment Utility
Excellent thankyou, watch the next post, i am sure it will be easy for you
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
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.

771 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

14 Experts available now in Live!

Get 1:1 Help Now