项目中的每个表单都需要数据库连接字符串吗? [英] Is database connection string required on every form within a project?

查看:66
本文介绍了项目中的每个表单都需要数据库连接字符串吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个由多个表单组成的项目。我在主表单的全局区域中声明了与数据库的连接。现在,当我从项目中的另一个表单触发查询时,它无法识别connection.Am我应该写连接在每个表格上都有所优势? Pl help ..

解决方案

你根本不应该在应用程序中编写连接字符串。相反,它应该存储在配置文件外面并从那里读取。



看看 Windows窗体的应用程序设置 [ ^ ]以及示例应用程序设置架构 [ ^ ]


否。您的应用程序应在其自己的类中具有应用程序的所有部分都使用的数据访问层。只有DAL需要知道如何连接数据的细节。


连接字符串应该在web.config或app.config文件中,并在代码中选取。 />


查看以下链接以获得更好的理解:

http://www.codeproject.com/Tips/416198/How -to-GET-连接字符串-从应用内-CONFIG-在-CS

I am developing a project which consists of multiple forms.I have declared connection to database in main form's global area .Now when I am firing query from another form within project, it is not recognizing connection.Am I supposed to write connection strin on every form? Pl help..

解决方案

You shouldn't write the connection string inside the application at all. Instead it should be stored outside in a configuration file and read from there.

Have a look at Application Settings for Windows Forms[^] and for an example Application Settings Architecture[^]


No. Your application should have a Data Access Layer in its own class that all the parts of the application use. Only the DAL needs to know the details of how to connect to the data.


connections string should be in the web.config or app.config files and picked up in the code.

have a look at the below link to get a better understanding:
http://www.codeproject.com/Tips/416198/How-to-get-Connection-String-from-App-Config-in-Cs


这篇关于项目中的每个表单都需要数据库连接字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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