Check if SQL Database Exists on a Server with vb.net

Good Morning! Well yesterday I told you I was considering doing a new project. It seems there is someone else who has done something similar and does even more than what I was thinking of doing. I decided that it is ok. Really I am writing it for myself as exercise for my coding skills so I am going to do it anyway. But I probably will get it anyway (am awaiting the download link from a friend) just cause it sounds really cool.

Anyway, today’s topic is to check to see if a SQL database already exists on a server. My thinking is when the user added a database we only want to let them add that database once. So here it is. Make it a great day!


Join me on Facebook

Public Shared Function CheckDatabaseExists(ByVal server As String, ByVal database As String) As Boolean
        Dim connString As String = ("Data Source="  _
                    + (server + ";Initial Catalog=master;Integrated Security=True;"))
        Dim cmdText As String = ("select * from master.dbo.sysdatabases where name=\’"  _
                    + (database + "\’"))
        Dim bRet As Boolean = false
        Using sqlConnection As SqlConnection = New SqlConnection(connString)
        sqlConnection.Open
        Using sqlCmd As SqlCommand = New SqlCommand(cmdText, sqlConnection)
        Using reader As SqlDataReader = sqlCmd.ExecuteReader
        bRet = reader.HasRows
        End Using
        End Using
        End Using
        Return bRet
    End Function

Technorati Tags: ,,,,,,,,,,,,,
,,,,,,,

Advertisements
  1. Check SQL Database Exists with C# « Kelly's Chronicles

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: