如何从UI层访问数据层中的连接字符串 [英] How to access Connection string in Data Layer From UI layer
本文介绍了如何从UI层访问数据层中的连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用3层架构开发应用程序,我想直接访问DL文件中的连接字符串(来自web.config)。
我可以在DL文件中访问它吗?
解决方案
你应该能够访问连接字符串使用ConfigurationManager.ConnectionStrings
属性。
假设你的web.config将有这样的条目:
< add name = ConnectionString connectionString = SERVER = yourserver; uid = username; pwd = yourpassword; database = yourdatabase /> ;
使用c#你可以这样读:
< span class =code-keyword> if (ConfigurationManager.ConnectionStrings [ ConnectionString ] == null )
{
throw new 异常( 未指定连接字符串);
}
return ConfigurationManager.ConnectionStrings [ ConnectionString的跨度>]的ConnectionString;
Hi,
I am using 3 tier architecture for developing application, and I want to access my connection string(from web.config)in DL file directly.
Can I access this in DL file?
解决方案
You should be able to access the connection string using theConfigurationManager.ConnectionStrings
property.
Assuming your web.config will have entry like this:
<add name="ConnectionString" connectionString="SERVER=yourserver;uid=username;pwd=yourpassword;database=yourdatabase"/>
using c# you can read it like this:
if (ConfigurationManager.ConnectionStrings["ConnectionString"] == null) { throw new Exception("Connection String not specified"); } return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
这篇关于如何从UI层访问数据层中的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文