Can I check if the database exists when creating the connection and if such a database does not exist, then run the script to create the database? I use Dapper. This is how I create a connection:
private static IDbConnection GetConnection(IConfigHelper config)
{
var factory = DbProviderFactories.GetFactory("System.Data.SqlClient");
var connection = factory.CreateConnection();
connection.ConnectionString = config.ConnectionString;
connection.Open();
return connection;
}
Different environments may be used in the future, so I need this check.
Include this in your db-creation-script and simply allways run it upon initializing your connection.
IF NOT EXISTS(SELECT * FROM sys.databases WHERE name = 'yourDB')
BEGIN
CREATE DATABASE yourDB
END