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);