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>
View Code

1、首先先在工程里添加system.configuration.dll程序集的引用

2、写代码读取配置参数

方法一:         string connStr= System.Configuration.ConfigurationManager.ConnectionStrings["strCon"].ToString();


方法二:          string connStr= ConfigurationManager.AppSettings["ConnectionString"];

 

posted @ 2018-04-27 20:27  congcongdi  阅读(557)  评论(0编辑  收藏  举报