简体   繁体   中英

Convert SQL Server database to compact: Error on running Executesql command

I am using below code to convert SQL server database to compact database. It does work fine if i build the application using visual studio 2010 as it targets it to .net 4.0 .But if i build or run it in visual studio(where i can target it to .net 3.5 or 2) It does not work properly. ExecuteSql(filename) fails to work. But if i access the file i can see the generated script. So i need to run the script manually in SSMS. So i need alternative to run this script generated which even works in .net 2 or 3.5. So please help me on this.

using ErikEJ.SqlCeScripting;
using System;
using System.IO;
using System.Data.SqlServerCe;

class Program
{
    static void Main(string[] args)
    {
        try
        {

            //--------Convert sql server express to compact db---------------------
        //http://erikej.blogspot.dk/2013/09/sql-server-compact-code-snippet-19.html
            using (IRepository serverRepository = new ServerDBRepository("Data Source=PCNAME\\SQLEXPRESS,25111;Initial Catalog=mydb;Persist Security Info=True;User ID=admin;password =mypassword"))
            {
                string fileName = Path.GetTempFileName();
                var generator = new Generator(serverRepository, fileName);
                generator.ScriptDatabaseToFile(Scope.SchemaData);

                var helper = new SqlCeHelper();
                var sqlCeConnectionString = @"Data Source=C:\Northwind.sdf";
                helper.CreateDatabase(sqlCeConnectionString);

                using (IRepository sqlCeRepository = new DBRepository(sqlCeConnectionString))
                {
                    sqlCeRepository.ExecuteSql(fileName);
                }
            }

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        Console.ReadKey();
    }
}

Use:

Process.Start("sqlcecmd.exe" "-d """Data Source=C:\\test2.sdf""" -i c:\input.txt");

And include sqlcecmd.exe (or sqlcecmd40.exe) with your app as content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM