仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 705|回复: 8
打印 上一主题 下一主题

[学习教程] MSSQL编程:web.config文件自界说设置节的利用办法...

[复制链接]
乐观 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 22:35:28 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
BlackHole黑洞引擎,写入的任何数据都会消失,一般用于记录binlog做复制的中继webweb.config文件自界说设置节的利用办法的一个复杂例子
用来演示的程序名为MyApp,Namespace也是MyApp

1。编纂web.config文件

增加以下内容,声明一个Section

<configSections>
<sectionname="AppConfig"type="MyApp.AppConfig,MyApp"/>
</configSections>

声了然一个叫AppConfig的Section

2。编纂web.config文件

增加以下内容,到场一个Section

<AppConfig>
<addkey="ConnectionString"value="thisisaConnectionString"/>
<addkey="UserCount"value="199"/>
</AppConfig>

这个Section包含两个Key

3。从IConfigurationSectionHandler派生一个类,AppConfig

完成Create办法,代码以下

publicclassAppConfig:IConfigurationSectionHandler
{
staticStringm_connectionString=String.Empty;
staticInt32m_userCount=0;
publicstaticStringConnectionString
{
get
{
returnm_connectionString;
}
}
publicstaticInt32UserCount
{
get
{
returnm_userCount;
}
}

staticStringReadSetting(NameValueCollectionnvc,Stringkey,StringdefaultValue)
{
StringtheValue=nvc[key];
if(theValue==String.Empty)
returndefaultValue;

returntheValue;
}

publicobjectCreate(objectparent,objectconfigContext,XmlNodesection)
{
NameValueCollectionsettings;

try
{
NameValueSectionHandlerbaseHandler=newNameValueSectionHandler();
settings=(NameValueCollection)baseHandler.Create(parent,configContext,section);
}
catch
{
settings=null;
}

if(settings!=null)
{
m_connectionString=AppConfig.ReadSetting(settings,"ConnectionString",String.Empty);
m_userCount=Convert.ToInt32(AppConfig.ReadSetting(settings,"UserCount","0"));
}

returnsettings;
}
}

我们把一切的设置都映照成响应的静态成员变量,而且是写成只读属性,如许程序经由过程

相似AppConfig.ConnectionString就能够会见,设置文件中的项目了

4。最初还要做一件事变

在Global.asax.cs中的Application_Start中增加以下代码

System.Configuration.ConfigurationSettings.GetConfig("AppConfig");

如许在程序启动后,会读取AppConfig这个Section中的值,体系会挪用你本人完成的IConfigurationSectionHandler接口来读取设置
支持大型的数据库。可以处理拥有上千万条记录的大型数据库。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-28 16:45

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表