简体   繁体   English

发布Windows应用程序后数据库路径错误

[英]Wrong database path after publishing the windows application

I developed a WPF application with MS Access database. 我用MS Access数据库开发了WPF应用程序。 Everything is working fine. 一切正常。 But when I tried to publish the app in order to create a setup file to use the application on other pcs, I am getting the following exception when I first run the app: 但是,当我尝试发布该应用程序以创建一个设置文件以在其他PC上使用该应用程序时,我在首次运行该应用程序时遇到以下异常:

System.Data.OleDbException: Could not find file 'C:\\Users\\me\\AppData\\Local\\Apps\\2.0\\77NYD08R.L6A\\Y93TLQGV.2TD\\pier..tion_b5378b5ea7b941ca_0001.0000_9b5c3ff3b52b7eb4\\Data. System.Data.OleDbException:找不到文件“C:\\用户\\我\\应用程序数据\\本地\\ APPS \\ 2.0 \\ 77NYD08R.L6A \\ Y93TLQGV.2TD \\ pier..tion_b5378b5ea7b941ca_0001.0000_9b5c3ff3b52b7eb4 \\数据。

I'm accessing the database in the code as follows: 我正在如下代码中访问数据库:

private void ConnectToDatabase()
{
     dBConnection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=MyProjectDB.mdb";
     dBConnection.Open();
}

And I added the database in bin/Debug folder. 然后将数据库添加到bin/Debug文件夹中。 Why it's trying to get the database file from AppData\\Local folder? 为什么要尝试从AppData\\Local文件夹中获取数据库文件? Should I move the database file to another folder? 我应该将数据库文件移动到另一个文件夹吗?

Basically there is what you should do: 基本上,您应该执行以下操作:

Do not leave the database file in your project folder, copy it elsewhere, like to Environment.SpecialFolder.ApplicationData . 不要将数据库文件留在项目文件夹中,而是将其复制到Environment.SpecialFolder.ApplicationData等其他位置。 You do it when the app is first launched and there no database file exists, so you copy the database from your project folder to your data folder. 您是在应用程序首次启动且没有数据库文件存在时执行此操作的,因此您将数据库从项目文件夹复制到数据文件夹。

There is quite a good answer, how to deal with the problem here . 这里有一个很好的答案,如何解决这个问题

If you need to write to the data deployed with your executable you should first copy it someplace you know the user will be able to write to, such as to Environment.SpecialFolder.ApplicationData, and write to the copy. 如果需要写入与可执行文件一起部署的数据,则应首先将其复制到您知道用户将能够写入的位置(例如Environment.SpecialFolder.ApplicationData),然后再写入副本。 Not only is DataDirectory not necessarily writable by users, it is part of the deployment and not part of the user data; DataDirectory不仅不一定是用户可写的,它还是部署的一部分,而不是用户数据的一部分; if you repair or uninstall your executable then DataDirectory gets reinstalled or deleted. 如果您修复或卸载可执行文件,则将重新安装或删除DataDirectory。 Users don't like it when you delete their data, so don't save it to DataDirectory 用户在删除数据时不喜欢它,因此请不要将其保存到DataDirectory

You can get the path of your ClickOnce executable via this command: 您可以通过以下命令获取ClickOnce可执行文件的路径:

 var exePath = System.IO.Path.GetDirectoryName(
               new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath);

See then this answer : Use something like that if the database should be used by multiple users of target computer: 请参见以下答案 :如果目标计算机的多个用户应使用该数据库,请使用类似的方法:

AppDomain.CurrentDomain.SetData("DataDirectory", Path.Combine(System.Environment.GetEnvironmentVariable("public"), YOUR_FOLDER_NAME));

Or just one database for one user: 或者只为一个用户提供一个数据库:

AppDomain.CurrentDomain.SetData("DataDirectory", Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),YOUR_FOLDER_NAME));

You should be aware that ClickOnce deployment method allows users that are not local admins to install your app, so it is better to choose such a folder on the target machine that should be by default writable for the end user. 您应该注意,ClickOnce部署方法允许非本地管理员的用户安装您的应用程序,因此最好在目标计算机上选择这样的文件夹,该文件夹默认情况下可为最终用户写入。

Then you use |DataDirectory| 然后使用|DataDirectory| in the connection string within your config file, like in the mentioned answer. 在配置文件中的连接字符串中,如上述答案中所示。

The DataDirectory would be a place where you copy the mdb file from exePath to your target data folder. DataDirectory将是您将mdb文件从exePath复制到目标数据文件夹的地方。

Your problem also is, that ClickOnce apps are installed in the user profile, so the database, if it is to be shared among different users of 1 computer, should be placed somewhere in c:\\users\\public folder. 您的问题还在于,ClickOnce应用程序已安装在用户配置文件中,因此,如果要在1台计算机的不同用户之间共享该数据库,则应将其放置在c:\\users\\public文件夹中的某个位置。 If it is to be used only by a single user, it can be placed in documents folder or somewhere in the current user's profile. 如果只供单个用户使用,则可以将其放置在documents文件夹中或当前用户配置文件中的某个位置。

So for a shared database your db folder where you copy your mdb after the first deployment could be something like this: 因此,对于共享数据库,您在第一次部署后将mdb复制到的db文件夹可能是这样的:

Path.Combine(System.Environment.GetEnvironmentVariable("public"), YOUR_FOLDER_NAME)

for a non-shared database it would be 对于非共享数据库,它将是

Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),YOUR_FOLDER_NAME)

There is usually a very good reason not to leave your data in your project folder as it is quite plainly recommended by Microsoft here : 通常,有一个很好的理由不要将数据保留在项目文件夹中,因为Microsoft明确建议在这里

When a ClickOnce application is uninstalled, its Data Directory is also removed. 卸载ClickOnce应用程序后,其数据目录也会被删除。 Never use the Data Directory to store end-user–managed data, such as documents. 切勿使用数据目录存储最终用户管理的数据,例如文档。

But if it is OK with you then you might be just fine changing the file attributes in Visual Studio, see this answer . 但是,如果可以,那么您可以在Visual Studio中更改文件属性就可以了, 请参见此答案

But also when you udpate the mdb file within your project, the locally installed data file gets rewritten by the published ClickOnce updates - one more link here , just to see that this is quite common approach to have data copied elsewhere by ClickOnce app at startup. 而且,当您在项目中添加mdb文件时,本地安装的数据文件也将由已发布的ClickOnce更新重写- 这里还有一个链接 ,只是看到这是在启动时由ClickOnce应用程序将数据复制到其他地方的非常普遍的方法。

You need to provide the location of you access database in the connection string 您需要在连接字符串中提供访问数据库的位置

dBConnection.ConnectionString  = "Provider=MICROSOFT.ACE.OLEDB.12.0; " +
                "Data Source=|DataDirectory|MyProjectDB.mdb"

|DataDirectory| | DataDirectory | needs to be a full relative path this is just a placeholder. 必须是一条完整的相对路径,这只是一个占位符。 So you can use 所以你可以使用

var path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

dBConnection.ConnectionString  = "Provider=MICROSOFT.ACE.OLEDB.12.0; " +
                "Data Source=" + path + "\MyProjectDB.mdb"

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

相关问题 在查找数据库和 xml 文件的路径时出现发布应用程序错误 - After publishing application error occurs in finding path to database and xml files Windows窗体应用程序与数据库发布 - Windows forms application with database publishing ASP.NET 应用程序在发布到 windows 服务器 2008 机器时使用了错误的文件路径 - ASP.NET application using the wrong file path when publishing to a windows server 2008 machine C#Windows Form应用程序未发布本地数据库(.mdf) - C# Windows Form Application is not publishing local database (.mdf) 使用本地数据库发布 Windows 窗体 - Publishing a Windows Form with Local Database 如何在使用相对路径发布应用程序后创建的应用程序文件中访问c#中的文件夹? - How to access a folder in c# in application files which is created after publishing application using relative path? 发布Silverlight应用程序后出现错误 - Error after publishing Silverlight application 安装Windows Service后获取xml文件的错误路径 - Getting wrong path of xml file after installing windows service 为什么发布后日期格式错误? - Why Is Date Format Wrong after publishing? 发布 Windows Forms .NET 应用程序后缺少 SQLite.Interop.dll - SQLite.Interop.dll missing after publishing a Windows Forms .NET application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM