Enterprise Data Access Application
•Reference the Data Access block assemblies
• Configure Data Access settings
• Add the required Namespace
• Create an instance of Database
• Perform actions using the Database instance
(1) including
Microsoft.Practices.EnterpriseLibrary.Common.dll Required
Microsoft.Practices.Unity.dll Required
Microsoft.Practices.Unity.Interception.dll Required
Microsoft.Practices.ServiceLocation.dll Required
Microsoft.Practices.EnterpriseLibrary.Data.dll Required
(2)For instance
<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</configuration>
</configSections>
<dataConfiguration defaultDatabase="BMPConfig"/>
<connectionStrings>
<add name="BMPConfig" connectionString="Data Source=xx.xx.xx.xx;Initial Catalog=xxxx;Persist Security Info=True;User ID=sa;Password=xxxxxx!" providerName="System.Data.SqlClient"/>
</connectionStrings>
the example in the book Enterprise Library5.0
[XML Configuration]
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.
EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.
Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<dataConfiguration defaultDatabase="EntLibBook-DataAccess" />
<connectionStrings>
<add name="EntLibBook-DataAccess" connectionString="server=.\
SQLEXPRESS;database=EntLibBook-DataAccess;Integrated Security=true;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
(3) for example
using Microsoft.Practices.EnterpriseLibrary.Data
(4)such as
a) Using the DatabaseFactory class
Database DBObj = DatabaseFactory.CreateDatabase();
Database db = DatabaseFactory.CreateDatabase("EntLibBook-DataAccess");
b)Using Unity service locator
Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("EntLibBook-DataAccess");
c)Using Unity container directly
This approach leverages
the dependency injection mechanism to create objects instead of explicitly creating
instances of concrete implementations.
var container = new UnityContainer();
container.AddNewExtension<EnterpriseLibraryCoreExtension>();
Database database = container.Resolve<Database>();
var container = new UnityContainer();
container.AddNewExtension<EnterpriseLibraryCoreExtension>();
Database database = container.Resolve<Database>("EntLibBook-
DataAccess");
use defaultDatabase
(5)
xxxxx
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步