这里记录一下,YZR.Data对系统兼容数据库在webconfig的配置处理.
<?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <configSections> <section name="YZRSetting" type="YZR.Config.YZRSetting, YZR.Config" /> </configSections> <connectionStrings> <add name="connString" connectionString="Data Source=127.0.0.1/orcl;Persist Security Info=True;User ID=YZR;Password=677714" providerName="YZROracle"/> </connectionStrings> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> </system.web> <YZRSetting> <base title="巴夫&巴夫" subTitle="YZR的博客" url="http://www.cnblogs.com/Francis_YZR"></base> <strConn> <!--<file name="headPhoto" path="upload/image/headPhoto" size="200"></file> <file name="album" path="upload/image/album" size="1024"></file>--> <conn Rname="Oracle" Rvalue="YZROracle"></conn> <conn Rname="Sqlserver" Rvalue="YZRSqlserver"></conn> </strConn> </YZRSetting> </configuration>
一开始的时候,我的做法是在appStart.cs中通过配置"RAtion"或者"RMtion"来决定采用Oracle,还是Sqlserver,但这样并不明智,因为需要改动项目代码,所以,我就想把决定数据库驱动的时机转移webconfig.通过providerName来指定数据库驱动.
如下:
<connectionStrings> <add name="connString"
connectionString="Data Source=127.0.0.1/orcl;Persist Security Info=True;User ID=YZR;Password=677714"
providerName="YZROracle"/> </connectionStrings>
然后,providerName提供出来之后,并未知道其有多少选择值,而且我希望也把它也配置起来,可以实现手动配置和任意添加删除,所以,我在wenconfig文件中加了自定义节点.
如下:
<YZRSetting> <base title="巴夫&巴夫" subTitle="YZR的博客" url="http://www.cnblogs.com/Francis_YZR"></base> <strConn> <!--<file name="headPhoto" path="upload/image/headPhoto" size="200"></file> <file name="album" path="upload/image/album" size="1024"></file>--> <conn Rname="Oracle" Rvalue="YZROracle"></conn> <conn Rname="Sqlserver" Rvalue="YZRSqlserver"></conn> </strConn> </YZRSetting>
这样的目的很明确,就是providerName的值是通过Rvalue决定的.
下面贴上YZRSetting.cs
namespace YZR.Config //好好研究一下ConfigurationSection { using System.Configuration; public class YZRSetting : ConfigurationSection { //base节点 [ConfigurationProperty("base")] public baseSection BaseSetting { get { return (baseSection)base["base"]; } } //fileUpload节点 [ConfigurationProperty("strConn")] public strConnSection StrConnSetting { get { return (strConnSection)base["strConn"]; } } } public class baseSection : ConfigurationElement { //title属性 [ConfigurationProperty("title", IsKey = true, IsRequired = true)] public string title { get { return (string)base["title"]; } set { title = value; } } //subTitle属性 [ConfigurationProperty("subTitle", IsRequired = false, DefaultValue = "")] public string subTitle { get { return (string)base["subTitle"]; } set { subTitle = value; } } //url属性 [ConfigurationProperty("url", IsRequired = true)] public string url { get { return (string)base["url"]; } set { url = value; } } } [ConfigurationCollection(typeof(connSection), AddItemName = "conn")]//子元素的声明conn public class strConnSection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new connSection(); } protected override object GetElementKey(ConfigurationElement element) { return ((connSection)element).Rname; } public connSection this[int index] { get { return (connSection)base.BaseGet(index); } } new public connSection this[string name] { get { return (connSection)base.BaseGet(name); } } } public class connSection : ConfigurationElement { //name属性 [ConfigurationProperty("Rname", IsKey = true, IsRequired = true)] public string Rname { get { return (string)this["Rname"]; } set { Rname = value; } } //value属性 [ConfigurationProperty("Rvalue", IsRequired = true)] public string Rvalue { get { return (string)this["Rvalue"]; } set { Rvalue = value; } } ////path属性 //[ConfigurationProperty("path", IsRequired = true)] //public string path { get { return (string)this["path"]; } set { path = value; } } ////size属性 //[ConfigurationProperty("size", IsRequired = true, DefaultValue = 1024)] //public int size { get { return (int)this["size"]; } set { size = value; } } } public class WebSettingManager { public static YZRSetting WebSetting = (YZRSetting)System.Configuration.ConfigurationManager.GetSection("YZRSetting"); } /* * * WebSettingManager.WebSetting.BaseSetting.title + " - " + WebSettingManager.WebSetting.BaseSetting.subTitle; WebSettingManager.WebSetting.BaseSetting.title; WebSettingManager.WebSetting.BaseSetting.subTitle; WebSettingManager.WebSetting.BaseSetting.url; WebSettingManager.WebSetting.strConnSetting;//数组的形式 */ }
在appStart.cs的处理如下:
string Title = WebSettingManager.WebSetting.BaseSetting.title + " - " + WebSettingManager.WebSetting.BaseSetting.subTitle; int count = WebSettingManager.WebSetting.StrConnSetting.Count; List<string> list = new List<string>(); for (int i = 0; i < count; i++) { connSection section=WebSettingManager.WebSetting.StrConnSetting[i]; list.Add(section.Rvalue); } //string strConn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["default"].ConnectionString; //string connString = System.Configuration.ConfigurationManager.ConnectionStrings["connString"].ToString(); string ProviderName = System.Configuration.ConfigurationManager.ConnectionStrings["connString"].ProviderName; string path = AppDomain.CurrentDomain.BaseDirectory; Assembly ass = Assembly.LoadFrom(path + "bin\YZR.Data.dll"); //Assembly ass = Assembly.Load("YZR.Entity"); //TODO:Oracle数据库使用RAtion,Sqlserver使用RMotion Type type = null; for (int i = 0; i < list.Count; i++) { if (list[i].Equals(ProviderName, StringComparison.OrdinalIgnoreCase)) { type = ass.GetType("YZR.Data." + "RAction"); break; } if (list[i].Equals(ProviderName, StringComparison.OrdinalIgnoreCase)) { type = ass.GetType("YZR.Data." + "RMotion"); break; } else throw new Exception("配置节点出错"); }
END!