0

I found the following query in order to find out if a database table was created already or not:

if db_id('thedbName') is not null
   --code mine :)
   print 'db exists'
else
   print 'nope'

Now I am wanting to use that same query within my VB.net application. This is the code I currently have elsewhere that connects to the database (that I am wanting to see if its there before doing all this):

Dim cn As SqlConnection = New SqlConnection("Data Source=DAVIDSDESKTOP;" & _
                                            "Initial Catalog=thedbName;" & _
                                            "Integrated Security=True;" & _
                                            "Pooling=False")

    Dim sql As String = "if db_id('thedbName') is not null " & vbCrLf & _
                            "Print() 'exists' " & vbCrLf & _
                        "else " & vbCrLf & _
                            "Print() 'nope'"

    Dim cmd As SqlCommand = New SqlCommand(sql, cn)

    cmd.Connection.Open()
    Dim blah As String = cmd.ExecuteNonQuery()
    cmd.Connection.Close()

Of course the issue with this is that I have to know the database name first in order to connect to the database.

I then seem to be able to connect to the master database using this:

Dim cn As SqlConnection = New SqlConnection("Data Source=DAVIDSDESKTOP;" & _
                                            "Integrated Security=True;" & _
                                            "Pooling=False")

    Dim sql As String = "if db_id('thedbName') is not null " & vbCrLf & _
                            "Print() 'exists' " & vbCrLf & _
                        "else " & vbCrLf & _
                            "Print() 'nope'"

    Dim cmd As SqlCommand = New SqlCommand(sql, cn)

    cmd.Connection.Open()
    Dim blah As String = cmd.ExecuteNonQuery()
    cmd.Connection.Close()

But that query seems to throw an error on Dim blah As String = cmd.ExecuteNonQuery() of:

Additional information: Incorrect syntax near ')'.

So I'm not all sure what I am missing in order to correct the issue with the query?

Need to know how to have the query come back and say 'exists' or 'nope'

7
  • Did you first run the query on your database before including it in your code? Commented Apr 24, 2016 at 23:36
  • 1
    Possible duplicate of How to check if a database and tables exist in sql server in a vb .net project? Commented Apr 24, 2016 at 23:37
  • Yes the query works just fine using SQL management studio. Commented Apr 24, 2016 at 23:37
  • @MrGadget not applicable since I am also wanting to return a 'exists' or 'nope'. No solution in that way in the link to the other post. Commented Apr 24, 2016 at 23:39
  • should be just Print exists without the parenthesis Commented Apr 24, 2016 at 23:47

2 Answers 2

2

Change Print() to Print (remove the parentheses.)


Better, don't use Print at all, use select.

Dim sql As String = "if db_id('thedbName') is not null " & vbCrLf & _
                        "select 'exists' " & vbCrLf & _
                    "else " & vbCrLf & _
                        "select 'nope'"

Dim blah As String = CType(cmd.ExecuteScalar(), string)

ExecuteNonQuery returns the number of affected rows for updates and inserts. But what you are executing is a query.

ExecuteScalar returns the first column of the first row selected. The query above only returns one row with one value, so that's what it will return.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for spotting that. No error not but it keeps returning -1 and not 'exists' or 'nope'
Great Scott! Ha. Thanks for the example code. It now works just fine!
0

or do it like this

select * from sys.databases where [name] = 'thedbName'

if it returns a row, then the database exists, if not then it doesn't.

To check if a table exists within a database, use this

select * from sys.objects where [name] = 'theTableName' and type_desc = 'USER_TABLE'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.