3

I have a DB connection string stored in web.config. I am creating a common library that requires the connection string. I could pass it in

Utility utilityToConnectToDb = new Utility("conString");

But, I have classes in the Library that call Utility. So I'll also need to give them knowledge of the connection string. This seems sloppy.

I am considering adding an XML file to the library and storing the connection string in this file.

Any better ideas or generally well accepted ways to solve this problem?

2
  • So, you want the utility to connect to use the connection string contained in the web.config without having to explicitly import the string through the constructor? Commented Aug 5, 2011 at 23:58
  • @EtherDragon - Yea. After some thought, I believe I can do this far easier than I thought. I believe I can just access the web.config at runtime through the constructor. I forgot that assemblies have access to the main config files. Commented Aug 6, 2011 at 0:08

3 Answers 3

1

it is difficult to maintain connections in library projects, easy way is access web config connection string from other libraries. add one util method to take the connection...

   private string GetWebConfigConnection()
    {

        string conString = string.Empty;
        System.Configuration.Configuration rootWebConfig =
            System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/WebSite21"); // give your web site name here

        System.Configuration.ConnectionStringSettings connString;
        if (rootWebConfig.ConnectionStrings.ConnectionStrings.Count > 0)
        {
            connString = rootWebConfig.ConnectionStrings.ConnectionStrings["MainConnStr"]; // give your connection string name here
            if (connString != null)
                conString=  connString.ConnectionString;
        }

        return conString;
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I like the idea. Why did you choose this implementation rather than using ConfigurationManager.ConnectionStrings[] ?
how many connection strings you have in your web.config? if it is more than one again you need to hard coding the connection string name many places which using this method.
1

you can put the connection string in to library app.config file as well. then you can use it as you used in the web project.

Comments

0

For changeable items like connection strings, I like building a serializable custom class, and writing a small UI to handle changing the configuration information. This way, if configuration information changes, it's relatively easy to change.

The configuration class can be known to all of the classes of the project. You can encrypt the data and even use hashing to verify the integrity of the values, if you want to go that far.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.