How to deploy a windows forms database application

2019-07-22 14:23发布

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条回答
forever°为你锁心
2楼-- · 2019-07-22 15:02

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

查看更多
地球回转人心会变
3楼-- · 2019-07-22 15:13

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

查看更多
虎瘦雄心在
4楼-- · 2019-07-22 15:14

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.

查看更多
登录 后发表回答