How to determinate if a database exist?

  • Thread starter Thread starter ad
  • Start date Start date
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
 
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
 
Back
Top