Tag Archives: sqlce

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);

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

Simple, easy & clear.