Create SqlCe database before integration test

7 Ott

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 Risposte to “Create SqlCe database before integration test”

  1. Alexander Byndyu ottobre 16, 2008 a 6:08 am #

    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?

Trackbacks/Pingbacks

  1. Plastic/Blog » Using SqlCe in TDD environment - ottobre 12, 2008

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

  2. Create Microsoft sql database before integration test « TDD developer - gennaio 9, 2009

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

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...

%d blogger cliccano Mi Piace per questo: