简体   繁体   English

将SQL Server数据库转换为紧凑型:运行Executesql命令时出错

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

I am using below code to convert SQL server database to compact database. 我正在使用下面的代码将SQL Server数据库转换为紧凑型数据库。 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. 如果我使用Visual Studio 2010构建应用程序时它工作正常,因为它的目标是.net 4.0。但是如果我在Visual Studio中构建或运行它(我可以将其定位到.net 3.5或2),它将无法正常工作。 ExecuteSql(filename) fails to work. ExecuteSql(filename)无法正常工作。 But if i access the file i can see the generated script. 但是,如果我访问文件,则可以看到生成的脚本。 So i need to run the script manually in SSMS. 所以我需要在SSMS中手动运行脚本。 So i need alternative to run this script generated which even works in .net 2 or 3.5. 所以我需要替代方法来运行生成的脚本,该脚本甚至可以在.net 2或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. 并在您的应用中包含sqlcecmd.exe(或sqlcecmd40.exe)作为内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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