TDD developer

October 7, 2008

Create SqlCe database before integration test

Filed under: TDD — makka @ 7:35 pm
Tags: , , ,

In these days I’m developing a smart client application that use a Sql Compact Edition database as local storage.
I’m using NHibernate to query the database and one of the things I need to check are my mapping files.
In order to accomplish this task I setup a series of integration tests. Once before all tests I run a method that create an empty database.
In fact using SqlCeEngine class I can create the empty database using CreateDatase method.
Later than with a simple SqlCeConnection I run a set of sql commands saved in .sql file in order to create all the objects inside my database.

const string connectionString = @"Data Source=|DataDirectory|demo.sdf";
var engine = new SqlCeEngine(connectionString);
engine.CreateDatabase(); 

string[] commands = File.ReadAllText(@"C:Projectsdemo002.CreateTables.sqlce").Split(';');
using (IDbConnection connection = new SqlCeConnection(connectionString))
{
    connection.Open();
    foreach (var commandText in commands)
    {
        if (string.IsNullOrEmpty(commandText)) continue;
        using (IDbCommand command = connection.CreateCommand())
        {
            command.CommandType = CommandType.Text;
            command.CommandText = commandText;
            command.ExecuteNonQuery();
        }
    }
}

Simple, easy & clear.

3 Comments »

  1. [...] can read the full article here: http://testdrivendevelopment.wordpress.com/2008/10/07/create-sqlce-database-before-integration-test/ Posted in .NET, ALT.NET, Database, TDD [...]

    Pingback by Plastic/Blog » Using SqlCe in TDD environment — October 12, 2008 @ 5:15 pm | Reply

  2. Hi,
    Interestion method for CE.

    We are using Transaction scope and execute all integration tests on live DB. But I think we could create new MOCK DB each time. How could you create DB “on-fly” for regular MS SQL?

    Comment by Alexander Byndyu — October 16, 2008 @ 6:08 am | Reply

  3. [...] my last post I shown how to create a SqlCe database “on the fly” once before all integration tests. [...]

    Pingback by Create Microsoft sql database before integration test « TDD developer — January 9, 2009 @ 6:27 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.