I am developing a windows forms application that needs to communicate with the SQL Server. I'm facing a problem when I deploy the application once the connection string is trying to connect to an invalid address.
I've already searched a lot and I found out the connection string must have the |DataDirectory|
directive. Now the .mdf file is located on the directory C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA
. Actually the connection string is:connectionString="Server=.\SQLExpress;AttachDbFilename=|DataDirectory|PDVDatabase.mdf;Database=PDVDATABASE;Trusted_Connection=Yes;"
The only way my app connects to the database is by the Server Explorer (I'm using Visual Studio 2013) where I get the static connection string of the .mdf file I set up in the app.config, but that way won't work after the deployment.
My question is: How do I do to connect my app after the deployment in order to communicate with the .mdf file? (I'm using a setup project for deployment). What's can be wrong?
Thanks.
A lot of things need to be addressed
Some links to help you in this task
To Attach and Detach a database information
For LOCALDB information
Connectionstring for LocalDb
Find Sql Server Instances Across your network
Maybe your troubles are caused by incorrect users and permissions setup in the database. You have to be sure, that the user, that is using your application after deployment, has right to connect and work on the database
You should put the connection string into App.config file of your application. There is a section meant for that in the config file.
That gives you a flexibility to change the config file after during the deployment of your application to make sure that the connection string points at the database in the user environment.
Later you can fetch the connection string in your C# code from the
System.Configuration.ConfigurationManager.ConnectionStrings
collection.Read on the internet on how to write different connection strings, ex here: http://www.connectionstrings.com/ and you should be able to write a correct connection string to the database.