Sql server ce 在发布时设置SQL Server Compact数据库的位置

Sql server ce 在发布时设置SQL Server Compact数据库的位置,sql-server-ce,publishing,local-database,Sql Server Ce,Publishing,Local Database,我有一个Winforms应用程序,它使用SQL Server Compact.SDF数据库来存储数据。调试时,该应用程序在Visual Studio中运行良好,但当我使用项目的“发布”选项并在安装后运行该程序时,总会收到一条错误消息“找不到数据库文件” 我设置连接字符串如下 string fileName = System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "Cellar.sdf"); connString

我有一个Winforms应用程序,它使用SQL Server Compact
.SDF
数据库来存储数据。调试时,该应用程序在Visual Studio中运行良好,但当我使用项目的“发布”选项并在安装后运行该程序时,总会收到一条错误消息“找不到数据库文件”

我设置连接字符串如下

string fileName = System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "Cellar.sdf");
connString = string.Format("Data Source={0};", fileName);
数据库
ceral.sdf
位于我的程序文件夹中


很明显,到数据库的路径是错误的,但是在使用“发布”功能时,有没有办法设置我希望应用程序安装的路径,或者找到复制数据库文件的位置?在桌面应用程序中包含本地数据库文件的正确方法是什么?

您可以包含DataDirectory宏,并在代码中将位置设置为合适的位置

connString = string.Format("Data Source=|DataDirectory|\{0};", fileName);

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        // This is our connection string: Data Source=|DataDirectory|\Chinook40.sdf
        // Set the data directory to the users %AppData% folder
        // So the Chinook40.sdf file must be placed in:  C:\\Users\\<Username>\\AppData\\Roaming\\
        AppDomain.CurrentDomain.SetData("DataDirectory", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
    }
connString=string.Format(“数据源=|数据目录| \{0};”,文件名);
私有void应用程序\u启动(对象发送方、StartupEventArgs e)
{
//这是我们的连接字符串:数据源=|数据目录|\Chinook40.sdf
//将数据目录设置为用户%AppData%文件夹
//因此Chinook40.sdf文件必须放在:C:\\Users\\\AppData\\Roaming中\\
AppDomain.CurrentDomain.SetData(“DataDirectory”,Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
}

这将要求您在初始应用程序启动期间将数据库文件复制到所需的位置(如果该位置尚未存在)。

您可以包括DataDirectory宏,并将该位置设置为代码中的适当位置

connString = string.Format("Data Source=|DataDirectory|\{0};", fileName);

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        // This is our connection string: Data Source=|DataDirectory|\Chinook40.sdf
        // Set the data directory to the users %AppData% folder
        // So the Chinook40.sdf file must be placed in:  C:\\Users\\<Username>\\AppData\\Roaming\\
        AppDomain.CurrentDomain.SetData("DataDirectory", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
    }
connString=string.Format(“数据源=|数据目录| \{0};”,文件名);
私有void应用程序\u启动(对象发送方、StartupEventArgs e)
{
//这是我们的连接字符串:数据源=|数据目录|\Chinook40.sdf
//将数据目录设置为用户%AppData%文件夹
//因此Chinook40.sdf文件必须放在:C:\\Users\\\AppData\\Roaming中\\
AppDomain.CurrentDomain.SetData(“DataDirectory”,Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
}

这将要求您在初始应用程序启动期间将数据库文件复制到所需的位置(如果它不存在)。

我可以运行一行代码检查它是否已经存在,或者将它复制到该位置,但是,如何复制它?我知道目的地,但如何确定安装时部署在何处?如果我能马上设置那个位置,我就不需要复制它了。在我看来,如果只需要选择一个位置就可以放置数据库,那么需要做很多工作。如果不需要数据库在应用程序更新后继续存在,只需使用DataDirectory,而不使用任何代码即可。我最后这样做了,
string fileName=System.IO.Path.Combine(ApplicationDeployment.CurrentDeployment.DataDirectory,“cill.sdf”);Console.WriteLine(文件名);connString=string.Format(“数据源={0};”,文件名)虽然我认为你的解决方案是一个更合适的方式来做它!我可以运行一行代码来检查它是否已经存在,或者在那里复制它,但是,我如何复制它呢?我知道目的地,但如何确定安装时部署在何处?如果我能马上设置那个位置,我就不需要复制它了。在我看来,如果只需要选择一个位置就可以放置数据库,那么需要做很多工作。如果不需要数据库在应用程序更新后继续存在,只需使用DataDirectory,而不使用任何代码即可。我最后这样做了,
string fileName=System.IO.Path.Combine(ApplicationDeployment.CurrentDeployment.DataDirectory,“cill.sdf”);Console.WriteLine(文件名);connString=string.Format(“数据源={0};”,文件名)虽然我认为你的解决方案是一个更合适的方式来做它!