如何在Windows应用程序中创建通用连接字符串 [英] how to create universal connection string in windows application

查看:78
本文介绍了如何在Windows应用程序中创建通用连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

value =数据源=。\SQLEXPRESS; AttachDbFilename = | DataDirectory | \ MyDatabase.mdf; Integrated Security = True; User Instance = True

解决方案

< blockquote> https://msdn.microsoft.com/en-us/library /ms254494(v=vs.110).aspx [ ^ ]


我正在使用Windows Application.In我正在使用SQL Express。当我尝试通过复制它从另一台计算机运行此应用程序然后它不会在数据库表中输入记录



并且可能存在问题。

.\SQLEXPRESS 具体说连接到这台计算机上的SQL Express实例 - 所以它会尝试。如果计算机已加载SQL,并且它可以找到数据库文件,它就可以工作。



但是如果你希望它能更新你计算机上的文件通过计算机上加载的SQL Express实例,您将需要一个不同的连接字符串。我强烈建议三件事:

1)不要试图为开发和生产保留相同的SQL实例:这是灾难的一个秘诀。记住在DELETE或UPDATE语句中忘记WHERE子句是多么容易 - 并且想一想如果你正在开发生产系统的话会有多乱......:OMG:

2)不要使用集成安全性进行生产:它允许完全访问数据库,这是非常危险的 - 特别是如果您的用户不是技术人员!

3)不要将文件附加到生产服务器:使数据库部分而只是连接到它。



所以修复它们,然后使用VS连接到生产数据库并获得一个连接字符串。



1)打开服务器资源管理器窗格。

2)右键单击数据连接并选择添加连接。

3)在随后的对话框中,选择您的数据源和数据库,指定安全信息,然后按测试连接按钮。

4)当连接有效时,按确定 。

5)在Server Explorer窗格中突出显示您的数据库,然后查看Pr operties窗格。将显示连接字符串的工作示例,您可以将其复制并粘贴到您的应用程序或配置文件中。


value="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyDatabase.mdf;Integrated Security=True;User Instance=True"

解决方案

https://msdn.microsoft.com/en-us/library/ms254494(v=vs.110).aspx[^]


"I am working with a Windows Application.In which I am using SQL Express. When i try to run this application from another computer by copying it then it does not enters record in database table"

And there is probably the problem.
.\SQLEXPRESS specifically says "connect to the SQL Express instance on this computer" - so it will try. And if the computer has SQL loaded, and it can find the DB file, it'll work.

But if you are expecting it to update the file on your computer via the SQL Express instance loaded on your computer then you will need a different connection string. And I would very strongly suggest three things:
1) Don't try to keep the same SQL instance for development and production: it's a recipe for disaster. Remember how easy it is to forget the WHERE clause in a DELETE or UPDATE statement - and think how messy that gets if you are developing against the production system... :OMG:
2) Don't use integrated security for production: it allows full access to the DB which is extremely dangerous - particularly if your users are not technical!
3) Don't Attach a file to production servers: make the DB part of SQL and just connect to it instead.

So fix them, and then use VS to connect to the production DB and get a connection string that way.

1) Open the Server Explorer Pane.
2) Right click "Data connections" and select "Add connection".
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK".
5) Highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file.


这篇关于如何在Windows应用程序中创建通用连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆