Solved

Check is SQL table exists via vb

Posted on 2014-02-24
3
214 Views
Last Modified: 2014-02-24
There are lots of blogs on this but all seem a little complicated.

I have tried this but i am not sure what to then check, should cmd be a boolean?  I guess not as its the result of a command line.

cmd = New SqlCommand("SELECT OBJECT_ID('temptbl_Stops')", con)
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()
If cmd = False Then
Else xxx

Thanks in advance
0
Comment
Question by:SweetingA
  • 2
3 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
Comment Utility
A SqlCommand object executes the command, that is all. In itself, it does not contain any data after the execution. All it knows is how to execute the command.

You need to get the result in a variable, a table or another type of container in order to know what the result is.

ExecuteNonQuery executes the command but does not return results, so it is useless with SELECT commands. It is used for INSERT, UPDATE and DELETE commands.

When you have a SELECT command that returns only one value, which is the case in your situation, you use ExecuteScalar. And in your case, ExecuteScalar returns a Null database value if it finds nothing. So your code should look like this:

            If IsDBNull(cmd.ExecuteScalar()) Then
                  MessageBox.Show("Table does not exist")
            Else
                  MessageBox.Show("Table does exist")
            End If
0
 

Author Comment

by:SweetingA
Comment Utility
Perfect once again, check out the last question of the evening
0
 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
Nothing is ever perfect (time is missing to give more complete information), but I try ;-)

I suggest that you give a look at the documentation and examine the different Execute... methods that are available for the SqlCommand object. You need to have a good idea of what each does in order to be comfortable working with ADO.NET.
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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

10 Experts available now in Live!

Get 1:1 Help Now