我正在开发一个需要与SQL Server通信的Windows窗体应用程序。在连接字符串尝试连接到无效地址后部署应用程序时,我遇到了问题。
我已经搜索了很多,我发现连接字符串必须有|DataDirectory|
指令。现在.mdf文件位于目录C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA
上。实际上连接字符串是:connectionString="Server=.\SQLExpress;AttachDbFilename=|DataDirectory|PDVDatabase.mdf;Database=PDVDATABASE;Trusted_Connection=Yes;"
我的应用程序连接到数据库的唯一方法是通过服务器资源管理器(我使用Visual Studio 2013),在那里我获取我在app.config中设置的.mdf文件的静态连接字符串,但是这种方式在部署后不会起作用。
我的问题是:如何在部署后连接我的应用程序以便与.mdf文件进行通信? (我使用安装项目进行部署)。什么可能是错的?
感谢。
答案 0 :(得分:1)
需要解决很多问题
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
一些帮助您完成此任务的链接
至Attach and Detach a database信息
对于LOCALDB信息
Connectionstring for LocalDb
Find Sql Server Instances Across your network
答案 1 :(得分:0)
您应该将连接字符串放入应用程序的App.config文件中。在配置文件中有一个用于该部分的部分。
<connectionStrings>
<add name="Connection String name" connectionString="..."/>
</connectionStrings>
这使您可以灵活地在部署应用程序之后更改配置文件,以确保连接字符串指向用户环境中的数据库。
稍后您可以从System.Configuration.ConfigurationManager.ConnectionStrings
集合中获取C#代码中的连接字符串。
在互联网上阅读如何编写不同的连接字符串,例如:http://www.connectionstrings.com/,您应该能够将正确的连接字符串写入数据库。
答案 2 :(得分:0)
也许您的麻烦是由数据库中不正确的用户和权限设置引起的。您必须确保部署后使用您的应用程序的用户有权连接和处理数据库