views:

202

answers:

1

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>
+11  A: 

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.

Canavar