Solved

Check is SQL table exists via vb

Posted on 2014-02-24
3
215 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

23 Experts available now in Live!

Get 1:1 Help Now