Skip to content

Scripting API samples

Erik Ejlskov Jensen edited this page Nov 13, 2016 · 4 revisions

To use the scripting library, add references to ISqlCeScripting.dll and SqlCeScripting.dll (and optionally SqlCeScripting40.dll for version 4.0 support).

The API exposes 4 repository implementations, DBRepository (for 3.5 SQL Server Compact files), DB4Repository (for SQL Server Compact 4.0 files), SQLiteRepository and ServerDBRepository (for SQL Server 2005 and later databases).

Generate a single table script

using ErikEJ.SqlCeScripting;
....
using (IRepository repository = new DBRepository(@"Data Source=C:\Northwind.sdf"))
{
    Generator generator = new Generator(repository, null);
    foreach (var tableName in repository.GetAllTableNames())
    { 
        generator.GenerateTableScript(tableName);
    }
    System.IO.File.WriteAllText(@"C:\script.sqlce", generator.GeneratedScript);    
}

Generate data (INSERTs) from all tables script

using ErikEJ.SqlCeScripting;
....
using (IRepository repository = new DBRepository(@"Data Source=C:\Northwind.sdf"))
{
    Generator generator = new Generator(repository, null);
    foreach (var tableName in repository.GetAllTableNames())
    { 
        generator.GenerateTableContent(tableName, false);
    }
    System.IO.File.WriteAllText(@"C:\script.sqlce", generator.GeneratedScript);    
}

Generate a diff script

using (IRepository sourceRepository = new DBRepository(@"Data Source=C:\data\source.sdf"))
{
    var generator = new Generator(sourceRepository);
    using (IRepository targetRepository = new DBRepository(@"Data Source=C:\data\target.sdf"))
    {
        SqlCeDiff.CreateDiffScript(sourceRepository, targetRepository, generator);
       //textBox.Text = generator.GeneratedScript;
    }

}

More examples here:

Script a database to SQLite

http://erikej.blogspot.dk/2013/03/sql-server-compact-code-snippet-of-week.html

Migrate a SQL Compact database to SQL Server

http://erikej.blogspot.dk/2013/03/sql-server-compact-code-snippet-of-week_8.html

Generate a CREATE TABLE script

http://erikej.blogspot.dk/2013/03/sql-server-compact-code-snippet-of-week_12.html

Script all data in a table

http://erikej.blogspot.dk/2013/05/sql-server-compact-code-snippet-of-week_9.html