美文网首页
C# 读取 appconfig文件配置数据库连接字符串,和配置文

C# 读取 appconfig文件配置数据库连接字符串,和配置文

作者: mhq_martin | 来源:发表于2018-07-04 10:41 被阅读0次

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings> --方式1
<add name="strCon" connectionString="server=127.0.0.1;user id=sa; password=csw; database=testDB;"/>
</connectionStrings>

<appSettings> --方式2
<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"];

相关文章

网友评论

      本文标题:C# 读取 appconfig文件配置数据库连接字符串,和配置文

      本文链接:https://www.haomeiwen.com/subject/hjxeuftx.html