部署后无法连接到本地数据库

时间:2023-01-17 16:55:53

I am writing a program in Visual C# 2015. I have created a local database from within the IDE and everything seems to work fine after building and running locally. However, after publishing the application and installing on another machine I get an argument exception error at attachdbfilename. I have included the database as a content file. The ClickOnce does copy the database to a folder under APPS in my Appdata folder. I have installed SQL Local DB edition on the target machine. The database name is ConnectionsDatabase.mdf.

我在Visual C#2015中编写了一个程序。我在IDE中创建了一个本地数据库,在本地构建和运行后,一切似乎都能正常工作。但是,在发布应用程序并在另一台机器上安装后,我在attachdbfilename上出现了一个参数异常错误。我已将数据库包含为内容文件。 ClickOnce会将数据库复制到Appdata文件夹中APPS下的文件夹中。我在目标计算机上安装了SQL Local DB版本。数据库名称为ConnectionsDatabase.mdf。

These are definitions in my Settings.Designer.cs file.

这些是我的Settings.Designer.cs文件中的定义。

[global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)]
        [global::System.Configuration.DefaultSettingValueAttribute("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\ConnectionSet" +
            "tings.mdf;Integrated Security=True; User Instance=True")]

[global::System.Configuration.DefaultSettingValueAttribute("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\ConnectionsDa" +
            "tabase.mdf;Integrated Security=True; User Instance=True")]

Not sure if they should both be there for just 1 database.

不确定它们是否应该只用于1个数据库。

This is what I use for connection string within the appropriate classes. using

这是我在适当的类中用于连接字符串的内容。运用

(SqlConnection con = new SqlConnection(Store_Switcher.Properties.Settings.Default.ConnectionsDatabaseConnectionString))

I am willing to share any and all project files that are needed. Keep in mind this is a small application that will be installed on several different machines. Please help!! This is driving me crazy!

我愿意分享所需的任何和所有项目文件。请记住,这是一个小型应用程序,将安装在几台不同的计算机上。请帮忙!!这真让我抓狂!

1 个解决方案

#1


1  

AttachDbFilename=|DataDirectory|\ConnectionSet" + "tings.mdf

should be

AttachDbFilename=|DataDirectory|\ConnectionData" + "base.mdf

in your designer file. or vice versa. you have a disconnect in file names.

在您的设计器文件中。或相反亦然。你有文件名断开连接。

EDIT: You mean "invalid value for key"? The problem sounds like whatever path you are using for the database on the target system doesn't match your development machine.

编辑:你的意思是“密钥的无效值”?问题听起来像您在目标系统上用于数据库的任何路径都与您的开发机器不匹配。

You even say "The ClickOnce does copy the database to a folder under APPS in my Appdata folder. " So maybe on your dev box you have c:\myapp\App_Data\ConnectionData.mdf and on target it is c:\myapp\App_data\APPS\ConnectionData.mdf after it gets deployed.This won't work. Get rid of the APPS, or change your code so the deployment folder is the same as on your dev box. This is a path issue.

你甚至说“ClickOnce会将数据库复制到我的Appdata文件夹中的APPS下的文件夹。”所以也许你的开发盒上有c:\ myapp \ App_Data \ ConnectionData.mdf,目标是c:\ myapp \ App_data \ APPS \ ConnectionData.mdf部署后。这不起作用。摆脱APPS,或更改您的代码,以便部署文件夹与开发框中的相同。这是一个路径问题。

#1


1  

AttachDbFilename=|DataDirectory|\ConnectionSet" + "tings.mdf

should be

AttachDbFilename=|DataDirectory|\ConnectionData" + "base.mdf

in your designer file. or vice versa. you have a disconnect in file names.

在您的设计器文件中。或相反亦然。你有文件名断开连接。

EDIT: You mean "invalid value for key"? The problem sounds like whatever path you are using for the database on the target system doesn't match your development machine.

编辑:你的意思是“密钥的无效值”?问题听起来像您在目标系统上用于数据库的任何路径都与您的开发机器不匹配。

You even say "The ClickOnce does copy the database to a folder under APPS in my Appdata folder. " So maybe on your dev box you have c:\myapp\App_Data\ConnectionData.mdf and on target it is c:\myapp\App_data\APPS\ConnectionData.mdf after it gets deployed.This won't work. Get rid of the APPS, or change your code so the deployment folder is the same as on your dev box. This is a path issue.

你甚至说“ClickOnce会将数据库复制到我的Appdata文件夹中的APPS下的文件夹。”所以也许你的开发盒上有c:\ myapp \ App_Data \ ConnectionData.mdf,目标是c:\ myapp \ App_data \ APPS \ ConnectionData.mdf部署后。这不起作用。摆脱APPS,或更改您的代码,以便部署文件夹与开发框中的相同。这是一个路径问题。