How to determinate if a database exist?

S

Sericinus hunter

ad said:
How can I determinate if a database exist in my SqlServer?

if exists select 1 from information_schema.schemata where catalog_name = 'your_db_name'
select 1
else
select 0
 
A

Andy L

For ADO.NET 2.0, SqlConnection (and all provider specific connection
classes) has a GetSchema method.
Before 2.0, only OleDbConnection provided Schema information.

'eg for ADO.NET 2.0
Using sc As New SqlConnection("Server=. ;Integrated Security=SSPI")
sc1.Open()
Dim dt As DataTable = sc1.GetSchema("databases")

For Each dr As DataRow In dt.Rows
For Each dc As DataColumn in dt.Columns
Console.WriteLine(dc.ColumnName & ":" & dr(dc))
Next
Next
End Using
 

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

Top