1.ConfigurationManager提供对客户端应用程序配置文件的访问。 其有两个属性1.ConnectionStrings 获取当前应用程序默认配置的 ConnectionStringsSection 数据。 方法一: string myConn =System.Configuration.ConfigurationManager.ConnectionStrings["sqlConnectionString"].ConnectionString; 方法二: string connString =System.Web.Configuration.WebConfigurationManager.ConnectionStrings["sqlConnectionString"].ToString(); web.config中的配置为: <configuration> <connectionStrings> <add name="sqlConnectionString" connectionString="LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;Initial Catalog=aspnetdb" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration> 2.AppSettings 获取当前应用程序默认配置的 AppSettingsSection 数据。 string myConn = ConfigurationManager.AppSettings["sqlConnectionString"].ToString(); web.config中的配置为: <configuration> <appSettings> <add key="sqlConnectionString" value="Monday, January 23, 2006 2:56:14 PM" /> </appSettings> </configuration>
文章摘自:http://www.cnblogs.com/_zjl/archive/2011/05/29/2062211.html