خرید بک لینک

Vote count: 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 coects to the database (that I am wanting to see if its there before doing all this):

Dim cn As SqlCoection = New SqlCoection("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.Coection.Open()
    Dim blah As String = cmd.ExecuteNonQuery()
    cmd.Coection.Close()

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

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

Dim cn As SqlCoection = New SqlCoection("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.Coection.Open()
    Dim blah As String = cmd.ExecuteNonQuery()
    cmd.Coection.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?

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: دوشنبه 6 ارديبهشت 1395 ساعت: 6:27

صفحه بندی