Solved

Check is SQL table exists via vb

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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