By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,890 Members | 1,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,890 IT Pros & Developers. It's quick & easy.

check SQL db exist...

P: n/a
Jon
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
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

"Jon" <Jo*@discussions.microsoft.com> wrote in message
news:CF**********************************@microsof t.com...
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

Nov 21 '05 #2

P: n/a
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.

"Michael C#" <xy*@abcdef.com> wrote in message
news:7B*******************@fe09.lga...
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

"Jon" <Jo*@discussions.microsoft.com> wrote in message
news:CF**********************************@microsof t.com...
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


Nov 21 '05 #3

P: n/a
Jon
thanks, i will give it a try.
"Michael C#" wrote:
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.

"Michael C#" <xy*@abcdef.com> wrote in message
news:7B*******************@fe09.lga...
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

"Jon" <Jo*@discussions.microsoft.com> wrote in message
news:CF**********************************@microsof t.com...
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



Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.