473,473 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

check SQL db exist...

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
3 1717
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Chin Chien Ping | last post by:
Hi, I looking for a solution for how to check if a connection to a remote computer exist or not using vb.net. I 'm writing a program which will request data from a remote sql server. I need...
1
by: Peter | last post by:
Hi, When my program starts up, it uses MAPILogonEx to connect to the default email profile (set by user). The problem is, if the email profile no longer exist, it would give an error "The...
2
by: adam | last post by:
hello What query shoul I send to SQL serwer ( in transact SQL language ) to check does some database exist on serwer ? It similar to problem "does some table exist in database" - resolve to it...
3
by: Dave | last post by:
How can i check to see if a link (file) exist while a web page is loading. If the page doesn't exist - i want the hyperlink to go an error page telling the user the link doesn't exist otherwise...
6
by: ItsMe | last post by:
Hi, I want to check whether the record exist or not based on my query, so i'll be using OleDbCommand. What is the best way to check on a huge table ? I used to write like this in VB6.0... ...
5
by: micklee74 | last post by:
hi in my code, i use dict(a) to make to "a" into a dictionary , "a" comes from user input, so my program does not know in the first place. Then say , it becomes a = { '-A' : 'value1' , '-B' :...
2
by: RLN | last post by:
Re: Access 2003 I have code to check to see if a table exists. There are several other tables (tblQ12, tblQ13, etc) that do already exist & this code runs fine; msgbox says the value returned is...
2
by: Earl | last post by:
Is there any sort of check that can be done to see if a particular instance of SQL Server is present? I already call a method to check for connection -- if no connection, I have the user re-enter...
1
by: Agnes | last post by:
pKcno = myReader.Item("kcno") <--it will return error when the field "kcno" doesn't exist any method to check the field exist or not first , if not exist, I want to skip the above statement. ...
3
by: maheswaran | last post by:
May this question exist....But i confused...I want to check whether the table is exist or not .... Like i run a project , on that project i create a temp tables while run time.... on that i want...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.