代码改变世界

微软企业库5.0学习笔记(三十三)数据访问模块

2010-05-14 07:08  Virus-BeautyCode  阅读(9270)  评论(13编辑  收藏  举报

  前言

  鉴于企业库5.0已经发布正式版,同时有广大读者的要求(臭屁一下,o(∩_∩)o...),后面文章的内容和代码将基于Enterprise Library5.0和Unity2.0来写,感谢大家的一贯支持。

  正文

  数据库访问模块都能实现哪些功能呢?数据库访问模块抽象类你正在使用的数据库,提供了一些列接口,使得你可以更容易的实现常用的数据库访问功能。例如:使用Database类填充DataSet数据集,用database类获取一个适当的Command实例,然后调用database的ExecuteDataSet方法,就可以填充数据集。不需要你调用DataAdapter的Fill方法。ExecuteDataSet方法管理数据库连接,实现了填充数据集所需要的所有工作。使用类似的方法,通过database类可以直接执行command,可以获取一个DataReader实例,可以用dataset中的数据更新数据库。模块也支持多个操作的事务,如果失败的话,可以回滚。

  除了使用ADO.NET也能完成的常用功能以外,模块还支持异步访问数据库(只要数据支持)。还可以返回客户端可以用LINQ查询的数据的序列对象形式。

  使用数据访问模块的主要优点,除了简化开发以外,它使得你可以创建一种provider独立的应用,可以很容易的更换不同的数据提供源。在大多数情况下,除非你在代码中指定了数据库类型,剩下的就是更改配置文件中的连接字符串配置节就可以了。不需要你修改代码中的sql查询和存储过程及其参数。

  数据访问模块提供的常用方法

  下面列出一些模块常用的获取数据、更新数据方法,其中有一些和直接使用ADO.NET中的方法很相似。

  

 

方法

功能

ExecuteDataset,创建,加载,返回数据集

LoadData,加载数据到一个已经存在的数据集

UpdateDataSet,使用已经存在的数据集更新数据库内容

填充一个数据集,使用数据集更新数据库

ExecuteReader,创建,返回一个provider独立的DbDataReader实例

从数据库读取多行数据

ExecuteNonQuery,执行command,返回数据库受影响的行数,可以通过output返回多个值

ExecuteScalar,执行command,返回单个值

执行command数据库命令对象

ExecuteSproAccessor,使用存储过程返回一个客户端可以查询的序列对象

ExecuteSqlStringAccessor,使用SQL语句返回一个客户端可以查询的序列对象

以序列对象的形式返回数据

ExecuteXmlReader,返回xml格式的数据,xmlReader类型,这个只能用在SQL Server数据库,通过SqlDatabase类调用,Database类中没有这个方法。

获取xml格式数据(只能用在SQL Server数据库)

GetStoredProcCommand,返回一个存储过程的数据库command对象

GetSqlStringCommand,返回一个SQL语句的数据库command对象

创建一个Command对象

AddInParameter,创建一个新的input参数,并且加入command的参数集合

AddOutParameter,创建一个新的output参数,并且加入command的参数集合

AddParameter,创建一个指定类型的参数,并且加入command的参数集合

GetParameterValue,以Object类型返回指定参数的值

SetParameterValue,给指定参数赋值

 

处理command的参数

CreateConnection,创建,返回当前数据库的连接,允许你通过这个链接初始化和管理数据库事务

处理数据库事务

 

      如果你使用SqlDatabase类的话,可以使用Begin和End类型的方法实现数据库异步操作。

      如何使用数据库访问模块

      1首先通过企业库的配置工具添加模块配置

      2在代码中添加如下代码

     

 

代码
static Database defaultDB = null;
static Database namedDB = null;
// 从容器中获取默认的数据库对象
// The actual concrete type is determined by the configuration settings.
defaultDB = EnterpriseLibraryContainer.Current.GetInstance<Database>();
// 从容器中获取指定名称的数据库对象
namedDB
= EnterpriseLibraryContainer.Current.GetInstance<Database>("ExampleDatabase");

 

       如果你需要使用ExecuteXmlReader方法,或者是需要使用SQL Server数据库对象的话,可以用下面的代码。

 

代码
static SqlDatabase sqlServerDB = null;
// Resolve a SqlDatabase object from the container using the default database.
sqlServerDB = EnterpriseLibraryContainer.Current.GetInstance<Database>()
as SqlDatabase;

// Assume the method GetConnectionString exists in your application and
// returns a valid connection string.
string myConnectionString = GetConnectionString();
SqlDatabase sqlDatabase 
= new SqlDatabase(myConnectionString);

 

      使用DataReader获取多行数据

 

代码
// Call the ExecuteReader method by specifying just the stored procedure name.
using (IDataReader reader = namedDB.ExecuteReader("MyStoredProcName"))
{
// Use the values in the rows as required.
}

// Call the ExecuteReader method by specifying the command type
// as a SQL statement, and passing in the SQL statement.
using (IDataReader reader = namedDB.ExecuteReader(CommandType.Text,
"SELECT TOP 1 * FROM OrderList"))
{
// Use the values in the rows as required ‐ here we are just displaying them.
DisplayRowValues(reader);
}

 

      根据参数获取多行数据

     

 

代码
using (IDataReader reader = defaultDB.ExecuteReader("ListOrdersByState",
new object[] { "Colorado" }))
{
// Use the values in the rows as required ‐ here we are just displaying them.
DisplayRowValues(reader);
}

 

     

     

      通过添加参数获取多行数据

代码
// Read data with a SQL statement that accepts one parameter.
string sqlStatement = "SELECT TOP 1 * FROM OrderList WHERE State LIKE @state";
// Create a suitable command type and add the required parameter.
using (DbCommand sqlCmd = defaultDB.GetSqlStringCommand(sqlStatement))
{
defaultDB.AddInParameter(sqlCmd, 
"state", DbType.String, "New York");
// Call the ExecuteReader method with the command.
using (IDataReader sqlReader = namedDB.ExecuteReader(sqlCmd))
{
Console.WriteLine(
"Results from executing SQL statement:");
DisplayRowValues(sqlReader);
}
}

 

     

 

 

 

代码
// Create a suitable command type and add the required parameter.
using (DbCommand sprocCmd = defaultDB.GetStoredProcCommand(storedProcName))
{
defaultDB.AddInParameter(sprocCmd, 
"state", DbType.String, "New York");
// Call the ExecuteReader method with the command.
using (IDataReader sprocReader = namedDB.ExecuteReader(sprocCmd))
{
Console.WriteLine(
"Results from executing stored procedure:");
DisplayRowValues(sprocReader);
}
}

 

      以对象形式返回数据

     

      上图是一个使用Accessor访问数据库,返回对象形式的数据的过程。

未完待续。。。。。。。。。。。。。。。。。。。。。。。。