Good Morning! I wanted to give a shout out to another blogger who linked to us yesterday here. It was so nice of him you know? Just amazing, the generosity of some folks out there!
Today’s code was a rewrite of the vb.net code here but using C#. One thing of note is that the Using keyword has a completely different meaning in C# so thus has been removed. It wasn’t really needed anyway.
Anyway, so make it a great day!
public Boolean testDatabaseExists(string server, string database)
String connString = (“Data Source=” + (server + “;Initial Catalog=master;Integrated Security=True;”));
String cmdText = (“select * from master.dbo.sysdatabases where name=\'” + (database + “\'”));
System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(connString);
System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand(cmdText, sqlConnection);
System.Data.SqlClient.SqlDataReader reader = sqlCmd.ExecuteReader();
bRet = reader.HasRows;
catch (Exception e)
bRet = false;
} //End Try Catch Block
if (bRet == true)
MessageBox.Show(“DATABASE DOES NOT EXIST”);
} //END OF IF
} //END FUNCTION