gpt4 book ai didi

c# - 已安装应用程序的 Visual Studio app.config 文件数据库路径

转载 作者:行者123 更新时间:2023-12-04 14:53:58 26 4
gpt4 key购买 nike

我在我的应用程序中使用本地数据库,当我生成安装文件时(通过 Installer Package ),在安装程序后它给出了数据库路径错误。

示例

an attempt to attach an auto-named database for file....
//OR
The given path format is not supported

one

我试图在 app.config 文件中编辑数据库路径,但每次都失败,默认情况下我的代码行是这样的:

<add name="SampleDatabaseWalkthrough.Properties.Settings.SampleDatabaseConnectionString"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\SampleDatabase.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />

我的应用程序安装在 C:\Program Files (86)\App_Folder\Setup 请注意, future 的用户可能会将其安装在自定义路径中,因此我需要一种获取动态路径的方法安装的应用程序

我的问题是如何获取应用程序安装路径以替换这部分 AttachDbFilename=|DataDirectory|\SampleDatabase.mdf

最佳答案

您可以尝试使用 AppDomain.CurrentDomain.SetData 方法来更改您的 mdf 文件路径。

因为,我不知道你是如何发布winform项目的。

我推荐你使用Clickonce发布。

首先,请在您的项目中包含您的 mdf 文件。

其次,您可以在发布后尝试使用以下代码更改安装路径。

 private void Form1_Load(object sender, EventArgs e)
{
if(System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
{
string path = ApplicationDeployment.CurrentDeployment.DataDirectory; //Get installed path
AppDomain.CurrentDomain.SetData("DataDirectory", path);//set the DataDirectory
}
}

最后,根据我的测试,在我发布并安装到另一台电脑后,可以从mdf文件中获取信息。

关于c# - 已安装应用程序的 Visual Studio app.config 文件数据库路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68507659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com