TheBeerHouse读书笔记(二)

Storing Connection Strings and Other Settings (web config)-Custom configuration sections

Connection String

<connectionStrings>
   <remove name="LocalSqlServer"/>
   <add name="LocalSqlServer" providerName="System.Data.SqlClient"
      connectionString="Data Source=.\SQLExpress;
      Integrated Security=True;User Instance=True;
      AttachDBFilename=|DataDirectory|TheBeerHouse.mdf" />
</connectionStrings>

Removing a connection string element means to remove a reference to an inherited connection string from the collection of connections strings. If there is an inherited connectionstring (not defined in current web.config, for example may be inherited from machine.config) named LocalSqlServer, it will no longer exist in current context. But this will not effect the original connectionstring (LocalSqlServer in machine.config in this case).

Configuration settings

public class SiteSection : ConfigurationSection
{
   [ConfigurationProperty("title", DefaultValue="Sample Title")]
   public string Title
   {
      get { return (string)base["title"]; }
      set { base["title"] = value; }
   }
  
   [ConfigurationProperty("homePage", IsRequired=true)]
   public HomePageElement HomePage
   {
      get { return (HomePageElement)base["homePage"]; }
   }
}
 public class HomePageElement : ConfigurationElement
{
   [ConfigurationProperty("showAdBanners", DefaultValue="true")]
   public bool ShowAdBanners
   {
      get { return (bool)base["showAdBanners "]; }
      set { base["showAdBanners "] = value; }
   }
}
Web.config
 
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
   <configSections> 
      <section name="site" 
         type="Company.Project.SiteSection, __code"/> 
   </configSections> 

 
   <site title="Some nice title for the site"> 
      <homePage showAdBanners="false" /> 
   </site> 
   <!-- other configuration sections... -->
</configuration>
 
To read
SiteSection site = (SiteSection)WebConfigurationManager.GetSection("site"); 
string title = site.Title; 
bool showAdBanners = site.HomePage.ShowAdBanners; 

转载于:https://www.cnblogs.com/jok141/archive/2011/08/25/2153134.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值