Solved

Check is SQL table exists via vb

Posted on 2014-02-24
3
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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