开发者

How to make the connection string visible throughout the solution (across layers)?

开发者 https://www.devze.com 2022-12-09 18:45 出处:网络
I have developed an Asp.net application using three tire architecture and i am reading the connetions string from web.conf file in the UI layer,

I have developed an Asp.net application using three tire architecture and i am reading the connetions string from web.conf file in the UI layer,

My problem is i have to pass the connection string as a parameter in all the function开发者_如何学Cs that are in the Bo layer.

Is there any work around for making the connection string visible throughout the Bo layer so that i dont need to send the connetions string as parameter in all my method calls.


Include the System.Configuration reference to all projects in which you want to access the connection string and use the following code to access your connection string.

ConfigurationManager.ConnectionString["MyConnection"].ConnectionString

It is advisable to use a helper class to access your connection string.

internal sealed class Utility
{
     public static string MyConnectionString{
          get{
               return ConfigurationManager.ConnectionString["MyConnection"].ConnectionString;
          }
     }
}

This will help you to move your connection string from web.config to any other location in future without breaking your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号