C# 读取 appconfig文件配置数据库连接字符串,和配置文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="strCon" connectionString="server=127.0.0.1;user id=sa; password=csw; database=testDB;"/> </connectionStrings> <appSettings> <add key="ConnectionString" value="server=127.0.0.1;user id=sa; password=csw; database=testDB;"/> <add key="ConStringEncrypt" value="true"/> </appSettings> </configuration>
1、首先先在工程里添加system.configuration.dll程序集的引用
2、写代码读取配置参数
方法一: string connStr= System.Configuration.ConfigurationManager.ConnectionStrings["strCon"].ToString(); 方法二: string connStr= ConfigurationManager.AppSettings["ConnectionString"];