Solved

Check if SQL table exists from vb.net

Posted on 2014-02-23
2
797 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
ID: 39881055
cmd = New SqlCommand("SELECT OBJECT_ID('temptbl_Stops')")

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

Author Closing Comment

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

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

838 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