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!
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)
Using sqlCmd As SqlCommand = New SqlCommand(cmdText, sqlConnection)
Using reader As SqlDataReader = sqlCmd.ExecuteReader
bRet = reader.HasRows