check SQL db exist...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi,

Could anyone pls tell me what is the best way to check if a database exist
on the SQL server or not?

any example code would be appreciated!

many thanks in advance.
Jon
 
Something like this should do the trick:

Dim SqlCon As SqlConnection
Dim DatabaseExists As Boolean = True
Try
SqlCon = New SqlConnection("SERVER=(local);" & _
"INITIAL CATALOG=database_name;" & _
"USER ID=mcoles;PASSWORD=1qaz1qaz;")
SqlCon.Open()
Catch ex As SqlException
If ex.Number = 4060 Then
DatabaseExists = False
Else
Throw (ex)
End If
Finally
If Not (SqlCon Is Nothing) Then
SqlCon.Dispose()
End If
End Try
 
Ooops. I set up a test account to do this. That username and password need
to be modified to yours to run it. Sorry about that.
 
thanks, i will give it a try.


Michael C# said:
Ooops. I set up a test account to do this. That username and password need
to be modified to yours to run it. Sorry about that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top