Enterprise Library 对大家来说应该不陌生,很早我就听说了这个东西,但一直没有时间来学习,最近终于抽出时间来学习Enterprise Library,现在就把我学习过程中的一些实例发上来,供大家参考.Enterprise Library现在已经有2.0版本了,但我也是后来才看到的,其他的就不多说了,先说1.0里面的Data Access Application Block,以后有时间在研究2.0.
本来先看的是其中的Configuration Application Block,但我在Web项目中调试写入XML时总是不能成功,后来查资料得知Web项目中的config文件不能通过程序来修改或创建,不知道是否确有此事,还望高手指点,另外还不明白的是 Configuration Application Block在我们实际项目的开发中能起到什么样的作用,昨天也没想通.
好了,我们来看看Data Access Application Block 吧,首先我们看一下如何用Enterprise Library 提供的配置工具来创建一个数据库实例.首先我们应该新建我们所需项目,然后在Enterprise Library的安装目录的Bin目录中找到EntLibConfig.exe文件,双击打开,并选择File-->Open Application,打开刚刚新建项目的Web.Config文件,如下:
然后,右键单击Application,选择New--> DataAccess Application Block,如下图:
然后会自动在Data Access Block 节点下生成三个Connection String(保存新建的数据库实例的连接字符串信息),DataBaseInstance(此节点存放数据库实例名以及其他相关信息),DataBase Types(节点存放数据库类型,如Sql Server 或 Oracle等).接着我们在 Connection String 下的 Sql Connection String 下配置好我们的数据库连接信息(包括Server,Database,uid,pwd等).
上面所说的都配置好了以后,我们就可以点击 File-->Save All或Ctrl + Shift + S,如果配置没有错误的话,就会在你的项目中生成一个dataConfiguration.config文件,内容如下:
<?
xml version="1.0" encoding="utf-8"
?>
<
dataConfiguration
>
<
xmlSerializerSection
type
="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null"
>
<
enterpriseLibrary
.databaseSettings xmlns:xsd
="http://www.w3.org/2001/XMLSchema"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
defaultInstance
="EnterpriseLibrary"
xmlns
="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/data"
>
<
databaseTypes
>
<
databaseType
name
="Sqlserver"
type
="Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase, Microsoft.Practices.EnterpriseLibrary.Data, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null"
/>
</
databaseTypes
>
<
instances
>
<
instance
name
="EnterpriseLibrary"
type
="Sqlserver"
connectionString
="Sql Connection String"
/>
</
instances
>
<
connectionStrings
>
<
connectionString
name
="Sql Connection String"
>
<
parameters
>
<
parameter
name
="database"
value
="EnterpriseLibrary"
isSensitive
="false"
/>
<
parameter
name
="Integrated Security"
value
="True"
isSensitive
="false"
/>
<
parameter
name
="pwd"
value
=""
isSensitive
="true"
/>
<
parameter
name
="server"
value
="shy"
isSensitive
="false"
/>
<
parameter
name
="uid"
value
="sa"
isSensitive
="false"
/>
</
parameters
>
</
connectionString
>
</
connectionStrings
>
</
enterpriseLibrary.databaseSettings
>
</
xmlSerializerSection
>
</
dataConfiguration
>
在Web.config文件中加入了下面内容:
<
configSections
>
<
section
name
="enterpriselibrary.configurationSettings"
type
="System.Configuration.IgnoreSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
/>
</
configSections
>
<
enterpriselibrary
.configurationSettings xmlns:xsd
="http://www.w3.org/2001/XMLSchema"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
applicationName
="Application"
xmlns
="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/configuration"
>
<
configurationSections
>
<
configurationSection
xsi:type
="ReadOnlyConfigurationSectionData"
name
="dataConfiguration"
encrypt
="false"
>
<
storageProvider
xsi:type
="XmlFileStorageProviderData"
name
="XML File Storage Provider"
path
="dataConfiguration.config"
/>
<
dataTransformer
xsi:type
="XmlSerializerTransformerData"
name
="Xml Serializer Transformer"
>
<
includeTypes
/>
</
dataTransformer
>
</
configurationSection
>
</
configurationSections
>
<
keyAlgorithmStorageProvider
xsi:nil
="true"
/>
<
includeTypes
/>
</
enterpriselibrary.configurationSettings
>
到此为止,我们使用Data Access Application Block 的配置文件就基本上配置好了,下面我们来看看DataAccess Application Block 的几种基本操作:
首先我们建立名为EnterpriseLibrary的数据库(这个应该在配置之前先建好的),在此数据库中建一张表,名为 Person .
需要引用的程序集如下:
Microsoft.Practices.EnterpriseLibrary.Data
Microsoft.Practices.EnterpriseLibrary.Configuration
1.ExecuteNonQuery
/**/
/// <summary>
/// 执行ExecuteNonQuery
/// </summary>
public
void
AddPerson()
{
Database db = DatabaseFactory.CreateDatabase("EnterpriseLibrary");
db.ExecuteNonQuery(CommandType.Text,"insert into person values(1,'pw',23,'男','3304122')");
}
2.ExecuteReader
/**/
/// <summary>
/// 执行ExecuteReader,返回Person列表
/// </summary>
/// <returns></returns>
public
IDataReader GetPersonList()
{
Database db = DatabaseFactory.CreateDatabase("EnterpriseLibrary");
string sql = "select * from person";
DBCommandWrapper dw = db.GetSqlStringCommandWrapper(sql);
return db.ExecuteReader(dw);
}
3.ExecuteDataSet
/**/
/// <summary>
/// 执行ExecuteDataSet,返回Person列表
/// </summary>
/// <returns></returns>
public
DataSet GetPersonList()
{
Database db = DatabaseFactory.CreateDatabase("EnterpriseLibrary");
string sql = "select * from person";
DBCommandWrapper dw = db.GetSqlStringCommandWrapper(sql);
return db.ExecuteDataSet(dw);
}
4.ExecuteScalar
/**/
/// <summary>
/// 执行ExecuteScalar,返回单一的值
/// </summary>
/// <returns></returns>
public
int
GetPersonNum()
{
Database db = DatabaseFactory.CreateDatabase("EnterpriseLibrary");
string sql = "select count(*) from person";
DBCommandWrapper dw = db.GetSqlStringCommandWrapper(sql);
return (int)db.ExecuteScalar(dw);
}
5.执行存储过程
//
执行存储过程
//
首先我们在数据库中建一个名为"SelectPersonBySex"的存储过程
//
如果有输入参数可以这样写:dw.AddOutParameter("@sex",System.Data.DbType.String,'男');
//
执行完存储过程后取输出参数的值的方法是:dw.GetParameterValue("@sex");
public
DataSet GetPersonListBySex(
string
sex)
{
Database db = DatabaseFactory.CreateDatabase("EnterpriseLibrary");
DBCommandWrapper dw = db.GetStoredProcCommandWrapper("SelectPersonBySex");
dw.AddInParameter("@sex",System.Data.DbType.String,sex);
return db.ExecuteDataSet(dw);
}
6.在程序中加入事务
public
void
InsertPerson()
{
//创建数据库实例
Database db = DatabaseFactory.CreateDatabase("EnterpriseLibrary");
//在程序中加入事务
using (IDbConnection conn = db.GetConnection())
{
//打开连接
conn.Open();
//建立一个事务
System.Data.IDbTransaction trans = conn.BeginTransaction();
try
{
//循环向person表插入记录
for(int i=2;i<10;i++)
{
db.ExecuteNonQuery(CommandType.Text,@"insert into person values(" + i + ",'pw1',23,'男','3304122')");
}
//提交事务
trans.Commit();
}
catch(Exception)
{
//回滚事务
trans.Rollback();
}
finally
{
//关闭连接
conn.Close();
}
}
}
关于DataAccess Application Block 的基本操作就说到这,以后再有更深的研究我继续往上发,还有想了解一下,学过Enterprise Library的朋友们对它的看法,欢迎在此发表你的个人意见.
Email:[email protected]