How to replace the connection string in the class library?

Source: Internet
Author: User
Tags connectionstrings

In ASP. NETProgramIn multi-layer development, we often write the data access layer in a class library. When the connection string is used in the class library, many people find the problem, because the class library finally gives others a DLL that does not contain our common web. config or app. config. In this case, when the database we use needs to change, we cannot simply change the connection string by modifying web. config or app. config.

I don't know how you solve this problem (share it). I am doing this:

1) after the class library is created, the connection string will be placed in the app. config file, for example:

Code
< Connectionstrings >
< Add Name = "Testproj. properties. settings. testconnectionstring"
Connectionstring = "Data Source =. \ sqlexpress; initial catalog = test; Integrated Security = true"
Providername = "System. Data. sqlclient"   />
</ Connectionstrings >

2) In our ASP. net program, if you need to replace the connection string in this DLL, we can. config overwrites the connection string (the same name must be used), so that the connection string can be replaced. For example:

Code
< Connectionstrings >
< Add Name = "Testproj. properties. settings. testconnectionstring"
Connectionstring = "Data Source =. \ sqlexpress; initial catalog = test222; Integrated Security = true"
Providername = "System. Data. sqlclient"   />
</ Connectionstrings >

 

In this case, the DLL uses the new database test222. The problem is not too big, but it is very practical. Hope to help you!

Have a nice day!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.