简体   繁体   中英

In C# , how can I read a connection string stored in my web.config file connection string?

In C# class library, how can I read a connection string stored in my web.config file connection string tag? As in:

<connectionStrings>
 <add name="CLessConStringLocal" connectionString="server=localhost;database=myDb;uid=sa;pwd=mypassword;"/>
</connectionStrings>

Use ConfigurationManager.ConnectionStrings collection to access the connection stings that stored in your configuration files.

For example :

string myConnectionString = ConfigurationManager
          .ConnectionStrings["CLessConStringLocal"].ConnectionString;

Note : Do not forget to reference to System.Configuration assembly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM