In my last post I shown how to create a SqlCe database “on the fly” once before all integration tests. Alexander asked me how can do the same thing but using a Microsoft Sql database. Let’ s how we can do it:
using (IDbConnection connection = new SqlCeConnection(masterConnectionString)) { connection.Open(); using (IDbCommand command = connection.CreateCommand()) { command.CommandType = CommandType.Text; command.CommandText = "CREATE DATABASE UnitTestDemo"; command.ExecuteNonQuery(); } } }
//open a new connection to UnitTestDemo database and create all objects
The only difference here is the statement used for the creation of the database. As you can see we need a connection to the master database in order to execute the CREATE DATABASE statement. Once we have the new empty database we can close this first connection, open a new one to test database and run all the scripts for the objects (tables, views, stored procedures, ecc.) creation.