Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Check is SQL table exists via vb

Posted on 2014-02-24
3
Medium Priority
?
234 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 2000 total points
ID: 39883756
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
ID: 39883840
Perfect once again, check out the last question of the evening
0
 
LVL 40
ID: 39883874
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 Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month12 days, 14 hours left to enroll

971 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