dll的app.config中的连接字符串 [英] Connection String in app.config of a dll

查看:100
本文介绍了dll的app.config中的连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个C#dll,该文件使用 app.config 文件中的连接字符串连接到数据库。

I created a C# dll that uses connection string from app.config file to connect to database.

我在网络中使用它应用程序(Visual Studio 2013,Windows 7)也可以连接到数据库,并且在 web.config 文件中具有自己的连接字符串。
但是,当Web应用程序使用dll时,dll无法从 app.config 中找到它所需的连接字符串。

I use it in a web application (Visual Studio 2013, Windows 7) that also connects to a database and has its own connection string in web.config file. However, when web app uses dll, the dll cannot find connection string that it needs from app.config.

我意识到该dll在 web.config 中寻找连接字符串。现在,我将该连接字符串添加到了 web.config ,并且工作正常。

I realized that dll was looking for the connection string in web.config. For now, I added that connection string to web.config and it is working fine.

我的问题是:

这样做是否正确?或者,dll有一种方法可以找到其 app.config 并从那里加载连接字符串?

Is it the correct way of doing it? Or, there is a way for the dll to find its app.config and load connection string from there?

推荐答案

实际上,您的dll(类库项目)中的app.config文件不会影响最终的主机应用程序(使用dll的主机)。因此,您将始终需要在主机应用程序的配置文件中放入正确的连接设置。例如,如果在Winform或WPF应用程序中使用Assembly / dll,则应将相关配置(如连接字符串)放入Winform或WPF应用程序的app.config文件中。如果在ASP.NET Web应用程序中使用了Assembly / dll,则我们应确保将相关设置放在Web应用程序的web.config文件中。

Actually the app.config file in your dll (class library project) won't affect the final host application ( in which the dll is used). So you will always need to put the proper connection settings in the host application's config file. For example, if you use the assembly/dll in winform or WPF application, then you should put the related configuration (like connection strings) in the winform or WPF app's app.config file. If the assembly/dll is used in ASP.NET web application, then we should make sure the related settings are put in the web.config file of the web application.

这篇关于dll的app.config中的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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