日期:2014-05-16  浏览次数:21063 次

Enterprise Library DataAccess(数据层)笔记(转)

1、前行引入命名空间:

using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.ServiceLocation;
using System.Data.Common;
using System.Data.SqlClient;

2、创建database实例,这里有两种方式

???? 第一种:Database db = DatabaseFactory.CreateDatabase("QuickStarts Instance");

???? 这里的QuickStarts Instance是配置文件中的 connectionStrings? -> add name。配置文件如下:

???? <configSections>

?????????? <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=null" requirePermission="true" />

????? </configSections>

<dataConfiguration defaultDatabase="Connection String" />
? <connectionStrings>
??? <add name="QuickStarts Instance" connectionString="Database=EntLibQuickStarts;Server=.;uid=sa;pwd=11"
??????? providerName="System.Data.SqlClient" />
? </connectionStrings>

????? 第二种方式:?Database DB = EnterpriseLibraryContainer.Current.GetInstance<Database>("QuickStarts Instance");

????? 但这种方式一定要加一个命名空间的引入:using Microsoft.Practices.ServiceLocation;

3、数据操作之IDataReader