"ConfigurationSettings已过时"全面解决方案
2013-12-31 15:26
267 查看
难度:3
前期准备:对app.config, web.config文件中配置数据库连接有一定了解。
结论
VS2003:string connStr=System.Configuration.ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"];
VS2005:string connStr=System.Configuration.ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ToString();
必须添加引用system.configuration.dll
定义
ConfigurationSettings 提供运行时对读取配置节和公共配置设置的支持。
ConfigurationManager 提供对客户端应用程序配置文件的访问。无法继承此类。
ConfigurationSettings.AppSettings 属性
获取配置文件应用程序设置节的只读 NameValueCollection。 (已过时)
[ObsoleteAttribute("This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings")]
命名空间:System.Configuration
程序集:System(在 system.dll 中)
--------------------------------------------
ConfigurationManager 类
命名空间:System.Configuration
程序集:System.Configuration(在 system.configuration.dll 中)使用ConfigurationManager的话,必须添加这个引用!!
使用 ConfigurationManager 类,可以访问计算机和应用程序的配置信息。ConfigurationManager 是处理客户端应用程序配置文件的首选方法;不推荐使用任何其他方法。对于 Web 应用程序,建议使用WebConfigurationManager 类。
ConfigurationManager.AppSettings 属性
获取当前应用程序默认配置的 AppSettingsSection 数据。
ConfigurationManager.ConnectionStrings 属性
获取当前应用程序默认配置的 ConnectionStringsSection 数据。
例子
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="CustomSection"
type="Samples.Config.CustomSection,
ConfigurationManager,
Version=1.0.0.0,
Culture=neutralPublicKeyToken=null"
allowDefinition="Everywhere"
allowExeDefinition="MachineToApplication"
restartOnExternalChanges="true" />
</configSections>
<connectionStrings>
<add name="ConnStr1"
connectionString="LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;Initial Catalog=aspnetdb"
providerName="System.Data.SqlClient" />
<add name="ConnStr2"
connectionString="LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;Initial Catalog=aspnetdb"
providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="AppSetting0"
value="Monday, February 28, 2005 1:27:59 PM" />
<add key="AppSetting1"
value="Monday, February 28, 2005 1:36:40 PM" />
</appSettings>
<CustomSection fileName="default.txt" maxUsers="1000"
maxIdleTime="00:10:00" />
</configuration>
在Web.Config文件中用appSetting的话
配置文件的 appSettings 节元素包含一系列名称(key)/值(value)对,可由应用程序用于任何目的。
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings>
<add key="ConnectionString" value="" />
</appSettings>
<system.web>
...
</system.web>
</configuration>
在Web.Config文件中用connectionStrings的话
<appSettings/>
<connectionStrings>
<!--链接Access数据库的链接字符串-->
<add name="DBConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:EPaperOA.mdb" providerName="System.Data.OleDb" />
<!--链接Sql2000数据库的链接字符串-->
<add name="SqlConnectionString" connectionString="Data Source=N4;Initial Catalog=EPaperOA;Persist Security Info=True;User ID=sa;Password=123456" providerName="System.Data.SqlClient" />
</connectionStrings>
前期准备:对app.config, web.config文件中配置数据库连接有一定了解。
结论
VS2003:string connStr=System.Configuration.ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"];
VS2005:string connStr=System.Configuration.ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ToString();
必须添加引用system.configuration.dll
定义
ConfigurationSettings 提供运行时对读取配置节和公共配置设置的支持。
ConfigurationManager 提供对客户端应用程序配置文件的访问。无法继承此类。
ConfigurationSettings.AppSettings 属性
获取配置文件应用程序设置节的只读 NameValueCollection。 (已过时)
[ObsoleteAttribute("This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings")]
命名空间:System.Configuration
程序集:System(在 system.dll 中)
--------------------------------------------
ConfigurationManager 类
命名空间:System.Configuration
程序集:System.Configuration(在 system.configuration.dll 中)使用ConfigurationManager的话,必须添加这个引用!!
使用 ConfigurationManager 类,可以访问计算机和应用程序的配置信息。ConfigurationManager 是处理客户端应用程序配置文件的首选方法;不推荐使用任何其他方法。对于 Web 应用程序,建议使用WebConfigurationManager 类。
ConfigurationManager.AppSettings 属性
获取当前应用程序默认配置的 AppSettingsSection 数据。
ConfigurationManager.ConnectionStrings 属性
获取当前应用程序默认配置的 ConnectionStringsSection 数据。
例子
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="CustomSection"
type="Samples.Config.CustomSection,
ConfigurationManager,
Version=1.0.0.0,
Culture=neutralPublicKeyToken=null"
allowDefinition="Everywhere"
allowExeDefinition="MachineToApplication"
restartOnExternalChanges="true" />
</configSections>
<connectionStrings>
<add name="ConnStr1"
connectionString="LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;Initial Catalog=aspnetdb"
providerName="System.Data.SqlClient" />
<add name="ConnStr2"
connectionString="LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;Initial Catalog=aspnetdb"
providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="AppSetting0"
value="Monday, February 28, 2005 1:27:59 PM" />
<add key="AppSetting1"
value="Monday, February 28, 2005 1:36:40 PM" />
</appSettings>
<CustomSection fileName="default.txt" maxUsers="1000"
maxIdleTime="00:10:00" />
</configuration>
在Web.Config文件中用appSetting的话
配置文件的 appSettings 节元素包含一系列名称(key)/值(value)对,可由应用程序用于任何目的。
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings>
<add key="ConnectionString" value="" />
</appSettings>
<system.web>
...
</system.web>
</configuration>
在Web.Config文件中用connectionStrings的话
<appSettings/>
<connectionStrings>
<!--链接Access数据库的链接字符串-->
<add name="DBConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:EPaperOA.mdb" providerName="System.Data.OleDb" />
<!--链接Sql2000数据库的链接字符串-->
<add name="SqlConnectionString" connectionString="Data Source=N4;Initial Catalog=EPaperOA;Persist Security Info=True;User ID=sa;Password=123456" providerName="System.Data.SqlClient" />
</connectionStrings>
相关文章推荐
- "ConfigurationSettings已过时"全面解决方案
- "ConfigurationSettings已过时"全面解决方案
- "通配符的匹配很全面, 但无法找到元素 'tx:annotation-driven' 的声明" 的解决方案
- "ConfigurationSettings已过时"全面解决方案
- win2003 iis6 的"请求的资源在使用中"(无法运行ASP程序)终极解决方案
- 关于eclipse的"serializing cdt project settings"解决方案
- "分布式事务"解决方案汇总 -- 2PC/TCC/事务消息/1PC
- DNN单击事件只有在"编辑"状态下才有效的解决方案
- "NHibernate.Exceptions.GenericADOException: could not load an entity" 解决方案
- json数据为null处理为""解决方案
- "由于这台计算机没有远程桌面客户端访问许可证,远程会话被中断"的解决方案
- "Procedure EntryPoint XXX Not Found In WS2_32.dll"的解决方案
- asp.net在发送邮件时出现"服务器响应为: You are not authorized to send mail, authentication is required "解决方案
- 关于"SAPI" not supported的解决方案
- Android - 覆盖安装APP时出现"应用未安装"问题的解决方案
- MSDN帮助文档 "无法显示该网页" 的问题解决方案(转)
- "ora-01502 索引或这类索引的分区处于不可用状态"的解决方案
- "只能在执行Render()的过程中调用RegisterForEventValidation" 解决方案
- "Insufficient RAM for Flash Algorithms"出错原因及解决方案
- GridView输入Excel时出现"System.OutOfMemoryException: Out of memory"及其解决方案。