简体   繁体   中英

Can't connect to local database after deployment

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.

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

[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.

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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