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

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


<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> </configSections> <connectionStrings> <add name="TQ电力.Properties.Settings.wx2010ConnectionString" connectionString="Data Source=*****;Initial Catalog=wx2010;Persist Security Info=True;User ID=sa;Password=******" providerName="System.Data.SqlClient" /> </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>
复制代码
复制代码
//访问数据库连接字符串
string connStr1 = System.Configuration.ConfigurationManager.ConnectionStrings["TQ电力.Properties.Settings.wx2010ConnectionString"].ToString(); //访问元应用程序配置 string connStr2 = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString(); MessageBox.Show(connStr1); MessageBox.Show(connStr2);
posted @ 2018-07-12 21:07  net-sky  阅读(1973)  评论(0编辑  收藏  举报