1

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.

3 Answers 3

1

A lot of things need to be addressed

1) You need a copy of your MDF and LDF files to be distributed with your app
2) You need to know if your user has Sql Server installed in its internal LAN or its PC
If the previous condition is true then
    3.1) You need to attach your copy to the end user Sql Server Instance
    3.2) You need to change your connection string to point to the end user Sql Server Instance
else 
    4.1) You need to distribute and install LOCALDB
    4.1) You need to prepare the connection string for LOCALDB 

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

Sign up to request clarification or add additional context in comments.

Comments

0

You should put the connection string into App.config file of your application. There is a section meant for that in the config file.

<connectionStrings>
     <add name="Connection String name" connectionString="..."/>
</connectionStrings>

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.

2 Comments

Thanks msporek, but I've already done this. The problem occurs when the project is deployed. The connection string must be flexible to spot the .mdf file that will be installed together with the project. How can I proceed?
So previously you had an MDF file in your development environment, and now the database is within a running SQL Server Express instance? If that's the case, then you cannot just connect to MDF file in your connection string. Instead you need to have sth like that: "Data Source=.\SQLEXPRESS;Initial Catalog=<YourDatabaseName>" and you also need to use provide security info in the connection string, either integrated security ("Integrated Security=true;"), or user & pass - depending on how the SQL Server Express is configured on the machine. Read the connectionstrings.com website.
0

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

1 Comment

for now, I deployed to my own PC just for test purposes, using the same SQL Server user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.