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

跟風遠走 提交于 2019-11-28 03:51:18

问题


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>

回答1:


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.



来源:https://stackoverflow.com/questions/1737393/in-c-sharp-how-can-i-read-a-connection-string-stored-in-my-web-config-file-con

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!