技术开发 频道

Enterprise Library 2.0 技巧:如何将配置信息保存到数据库中


IT168 技术文档】

摘要:

    使用过Enterprise Library的朋友可能都知道,所有的配置信息都是放在了配置文件里面(应用程序配置文件或者外部配置文件),大家有没有想过把配置信息保存到数据库中呢?

主要内容

1.编译SqlConfiguration
2.创建数据表和存储过程
3.配置应用程序
4.使用应用程序块 
  
    使用过Enterprise Library的朋友可能都知道,所有的配置信息都是放在了配置文件里面(应用程序配置文件或者外部配置文件),大家有没有想过把配置信息保存到数据库中呢?在Enterprise Library用ConfigurationSource替代了Configuration Application Block,看一下ConfigurationSource的结构图:



EL默认的是System Configuration Source,关于如何使用File Configuration Source,在技巧(1)里面已经说过了,下面我们具体看一下使用Sql Configuration Source。


1.编译SqlConfiguration

在安装目录的QuickStarts文件夹里面,编译后拷贝如下文件到bin目录中

Microsoft.Practices.EnterpriseLibrary.SqlConfigurationSource.dll Microsoft.Practices.EnterpriseLibrary.SqlConfigurationSource.Design.dll Microsoft.Practices.EnterpriseLibrary.ConfigurationSource.SQL.XML Interop.MSDASC.dll


2.创建数据表和存储过程

    运行CreateSqlConfiguration.cmd,注意在这之前可能要先用记事本打开SqlConfiguration.sql文件,另存为Unicode格式,如下图:



    默认将安装在Northwind数据库中,安装完成后数据库中有一张   Configuration_Parameter表和四个相关的存储过程。





3.配置应用程序

    经过了上面两步之后,就可以开始配置应用程序了。新建一个项目,添加App.config,使用EntLibConfig.exe打开,在新建Configuration Source的时候,会发现菜单中多了一项Sql Configuration Source: 

    选择之后,出现如下界面,这时需要设置Sql Configuration Source的参数,如下图所示设置存储过程名称:


 
设置完成后,在Configuration Source的SelectedSource节点选中Sql Configuration Source:



这里为了测试,新建一个Logging Application Block。(当有Data Access Application Block时保存总会报错,不知道是什么问题?)保存后会在数据库中多出一条记录:






4.使用应用程序块

当配置完成后,看看App.config配置文件

<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourc eSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" /> </configSections> <enterpriseLibrary.ConfigurationSource selectedSource="Sql Configuration Source"> <sources> <add name="Sql Configuration Source" t="" ype="Microsoft.Practices.EnterpriseLibrary.SqlConfigurationSource.SqlConfigurationS ource, Microsoft.Practices.EnterpriseLibrary.SqlConfigurationSource, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" connectionString="Password=19811218;Persist Security Info=True;User ID=sa;Initial Catalog=Northwind;Data Source=RJ-097" getStoredProcedure="EntLib_GetConfig" setStoredProcedure="EntLib_SetConfig" refreshStoredProcedure="UpdateSectionDate" removeStoredProcedure="EntLib_RemoveSection" /> <add name="System Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfiguratio nSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" /> </sources> </enterpriseLibrary.ConfigurationSource> </configuration>
编写一个简单的日志应用程序:

class Program { static void Main(string[] args) { LogEntry log = new LogEntry(); log.Message = "This is a test!"; Logger.Write(log); } }
运行程序后,在trace.log中记录了这样的一条日志信息

----------------------------------------

General Information: 0 : Timestamp: 2006-7-4 8:02:05

Message: This is a test!

Category: General

Priority: -1

EventId: 0

Severity: Information

Title:

Machine: RJ-097

App Domain: EntLibDemo1.vshost.exe

ProcessId: 5896

Process Name: D:\Visual Studio2005 Project\EntLibDemo1\EntLibDemo1\bin\Debug\EntLibDemo1.vshost.exe

Thread Name: 

Win32 ThreadId:4304

Extended Properties: 

----------------------------------------

    我们就可以把配置信息保存到了SQL Server数据库中,也许有人觉得这个技巧不存在什么实际意义,但是特殊情况下,当你需要这样实现的时候,别忘了这篇Post。
0
相关文章