微软企业库 数据访问模块2

       数据库访问模块都能实现哪些功能呢?数据库访问模块抽象类你正在使用的数据库,提供了一些列接口,使得你可以更容易的实现常用的数据库访问功能。例如:使用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在代码中添加如下代码

     

 

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> 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数据库对象的话,可以用下面的代码。

 

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> 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获取多行数据

 

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> //  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);
}

 

      根据参数获取多行数据

     

 

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> 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);
}

 

     

     

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

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> //  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);
}
}

 

     

 

 

 

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> //  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访问数据库,返回对象形式的数据的过程。

 

转自:http://www.cnblogs.com/virusswb/archive/2010/05/14/Enterprise-Library-DataAccessBlock-3.html

 

 

以对象形式从数据库获取数据

  现代的很多编程技术都集中在“数据就是对象”这个概念。如果你在应用层之间使用Data Transfer Objects (DTOs)传输数据的话,这个方法就很有用,使用ORM实现一个数据访问层,或者是客户端查询技术,例如LINQ。

  数据库访问模块实现了这个功能,允许你执行SQL或者是存储过程,可以返回一个对象序列,但是要求序列实现IEnumerable接口。

  关于Accessors

  模块提供了两个方法来实现这种查询要求,SprocAccessor和SqlStringAccessor。你可以使用Database类的ExecuteSprocAccessor和ExecuteSqlStringAccessor,或者是创建一个Accessor,然后调用它的Execute方法。

  下面是一张查询示意图

  

  Accessor使用另外两个对象,一个用来管理传入accessor的参数,一个用来将数据库返回的数据行映射成客户端需要的对象。

  如果你没有指定一个参数映射,accessor会使用默认的参数映射。默认情况下这个功能只能用于执行SQL Server和Oracle的存储过程。不能执行SQL 语句,或者是其他的数据库和provider,如果需要的话,可以自定义参数映射器。

  如果你没有指定一个对象映射,模块使用默认的对象映射,根据列的名称映射到对象的属性。你也可以自定义对象映射,将列映射到指定的对象属性上去。

  创建和执行Accessor

  

 

复制代码
代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> //  Create an object array and populate it with the required parameter values
object[]  params =  new  object[] {  " %bike% " };
//  Create and execute a sproc accessor that uses the default
//  parameter and output mappings.
var productData = defaultDB.ExecuteSprocAccessor<Product>( " GetProductList ",
params);
//  Perform a client‐side query on the returned data. Be aware that
//  the orderby and filtering is happening on the client, not in the database.
var results = from productItem  in productData
where productItem.Description !=  null
orderby productItem.Name
select  new { productItem.Name, productItem.Description };
//  Display the results
foreach (var item  in results)
{
A Guide to Developing with Enterprise Library  5.0  41
Console.WriteLine( " Product Name: {0} ", item.Name);
Console.WriteLine( " Description: {0} ", item.Description);
Console.WriteLine();
}
复制代码

 

 

  上面假设由一个Product类,有ID,Name,Description三个属性。

  创建和使用映射Mappers

  在某些情况下,你需要自定义参数映射,将你的参数传递给要执行查询的accessor。这种典型的情况发生在,你需要执行一个SQL语句,和一个不支持参数的数据库进行交互,或者是默认的参数映射的参数个数或者是类型不匹配。参数映射类需要实现IParameterMapper接口,有一个AssignParameters方法引用了Command对象作为参数,你需要做的就是将需要的参数加入Command对象的Parameters集合。

  在更多的情况你需要一个自定义对象映射。为了帮助你完成这个需求,模块提供了MapBuilder类,用它可以创建列和对象属性的映射关系。

  默认情况,accessor可以返回一个简单的对象序列。但是,有时候需要返回一个复杂的对象序列,例如,返回Orders的同时返回相关的OrderLines。简单的映射不能满足这样的需求,MapBuilder类也不能满足了。你需要实现IResultSetMapper接口,自定义对象映射关系。

  获取XML数据

  企业库提供了ExecuteXmlReader方法返回一个XmlReader对象。目前为止,还只能支持SQL Server数据库,意味着你只能用SqlDatabase类。

  你需要把Database转换成SqlDatabse,或者是用构造函数直接创建一个SqlDatabse。

  

 

复制代码
代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> static SqlDatabase sqlServerDB =  null;
//  Resolve a SqlDatabase object from the container using the default database.
sqlServerDB
= EnterpriseLibraryContainer.Current.GetInstance<Database>()  as SqlDatabase;
//  Specify a SQL query that returns XML data
string xmlQuery =  " SELECT * FROM OrderList WHERE State = @state FOR XML AUTO ";
//  Create a suitable command type and add the required parameter
//  NB: ExecuteXmlReader is only available for SQL Server databases
using (DbCommand xmlCmd = sqlServerDB.GetSqlStringCommand(xmlQuery))
{
xmlCmd.Parameters.Add( new SqlParameter( " state "" Colorado "));
using (XmlReader reader = sqlServerDB.ExecuteXmlReader(xmlCmd))
{
while (!reader.EOF)  //  Iterate through the elements in the XmlReader
{
if (reader.IsStartElement())
{
Console.WriteLine(reader.ReadOuterXml());
}
}
}
}
复制代码

 

 

  获取单个值

  

 

复制代码
代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> //  Create a suitable command type for a SQL statement
//  NB: For efficiency, aim to return only a single value or a single row.
using (DbCommand sqlCmd
= defaultDB.GetSqlStringCommand( " SELECT [Name] FROM States "))
{
//  Call the ExecuteScalar method of the command
Console.WriteLine( " Result using a SQL statement: {0} ",
defaultDB.ExecuteScalar(sqlCmd).ToString());
}
//  Create a suitable command type for a stored procedure
//  NB: For efficiency, aim to return only a single value or a single row.
using (DbCommand sprocCmd = defaultDB.GetStoredProcCommand( " GetStatesList "))
{
//  Call the ExecuteScalar method of the command
Console.WriteLine( " Result using a stored procedure: {0} ",
defaultDB.ExecuteScalar(sprocCmd).ToString());
}
复制代码

 

  异步获取数据

  1)准备配置文件

  需要在连接字符串中添加Asynchronous Processing=true或者是async=true。

  

 

复制代码
代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--><connectionStrings>
<add name= " AsyncExampleDatabase "
connectionString= " Asynchronous Processing=true; Data Source=.\SQLEXPRESS;
Initial Catalog= " MyDatabase "; Integrated Security=True; "
providerName= " System.Data.SqlClient " />
...
</connectionStrings>
复制代码

 

  另外,企业库中的异步访问数据库只支持SQL Server。在Database类由一个属性SupportsAsync,可以用来查询当前数据库是否支持异步访问。

  

 

 

 

复制代码
代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> private  static  bool SupportsAsync(Database db)
{
if (db.SupportsAsync)
{
Console.WriteLine( " Database supports asynchronous operations ");
return  true;
}
Console.WriteLine( " Database does not support asynchronous operations ");
return  false;
}
复制代码

 

  使用异步访问数据库通常需要一个callback,在调用端会打开另外一个线程。这个callback通常不会直接访问winform和wpf的用户界面。你需要在用户界面添加事件,然后通过委托的方式更新用户界面。

  委托可以使用Lambda表达式实现。

 

复制代码
代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->DbCommand cmd = asyncDB.GetStoredProcCommand( " ListOrdersSlowly ");
asyncDB.AddInParameter(cmd,  " state ", DbType.String,  " Colorado ");
asyncDB.AddInParameter(cmd,  " status ", DbType.String,  " DRAFT ");
//  Execute the query asynchronously specifying the command and the
//  expression to execute when the data access process completes.
asyncDB.BeginExecuteReader(cmd,
asyncResult =>
{
//  Lambda expression executed when the data access completes.
try
{
using (IDataReader reader = asyncDB.EndExecuteReader(asyncResult))
{
A Guide to Developing with Enterprise Library  5.0  47
Console.WriteLine();
DisplayRowValues(reader);
}
}
catch (Exception ex)
{
Console.WriteLine( " Error after data access completed: {0} ", ex.Message);
}
},  null);
复制代码

  

 

  异步获取对象形式返回的数据

  

 

复制代码
代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> object[] paramArray =  new  object[] {  " %bike% "20 };
//  Create the accessor. This example uses the simplest overload.
var accessor = asyncDB.CreateSprocAccessor<Product>( " GetProductsSlowly ");
//  Execute the accessor asynchronously specifying the callback expression,
//  the existing accessor as the AsyncState, and the parameter values array.
accessor.BeginExecute(
asyncResult =>
//  Lambda expression executed when the data access completes.
try
{
//  Accessor is available via the asyncResult parameter
var acc = (IDataAccessor<Product>) asyncResult.AsyncState;
//  Obtain the results from the accessor.
var productData = acc.EndExecute(asyncResult);
//  Perform a client‐side query on the returned data.
//  Be aware that the orderby and filtering is happening
//  on the client, not inside the database.
var results = from productItem  in productData
where productItem.Description !=  null
orderby productItem.Name
select  new { productItem.Name, productItem.Description };
//  Display the results
Console.WriteLine();
foreach (var item  in results)
{
Console.WriteLine( " Product Name: {0} ", item.Name);
Console.WriteLine( " Description: {0} ", item.Description);
Console.WriteLine();
}
}
catch (Exception ex)
{
Console.WriteLine( " Error after data access completed: {0} ", ex.Message);
}
}, accessor, paramArray);
复制代码

 

  更新数据库

  

 

复制代码
代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->string oldDescription
= "Carries 4 bikes securely; steel construction, fits 2\" receiver hitch.";
string newDescription = "Bikes tend to fall off after a few miles.";
// Create command to execute the stored procedure and add the parameters.
DbCommand cmd = defaultDB.GetStoredProcCommand("UpdateProductsTable");
defaultDB.AddInParameter(cmd, "productID", DbType.Int32, 84);
defaultDB.AddInParameter(cmd, "description", DbType.String, newDescription);
// Execute the query and check if one row was updated.
if (defaultDB.ExecuteNonQuery(cmd) == 1)
{
// Update succeeded.
}

else
{
Console.WriteLine("ERROR: Could not update just one row.");
}
// Change the value of the second parameter
defaultDB.SetParameterValue(cmd, "description", oldDescription);
// Execute query and check if one row was updated
if (defaultDB.ExecuteNonQuery(cmd) == 1)
{
// Update succeeded.
}
else
{
Console.WriteLine("ERROR: Could not update just one row.");
}
复制代码
 
 
       使用DataSet进行工作

  使用Database类的ExecuteDataSet方法获取DataSet对象,在DataSet对象中,默认的表名称依次为Table,Table1,Table2.。。。。。。。。。。。。

  如果你想要将数据加载到一个已经存在的DataSet对象中,可以使用LoadDataSet方法。

  

复制代码
代码
<!--<br/ /> <br/ /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ /> http://www.CodeHighlighter.com/<br/ /> <br/ /> -->DataSet productDataSet;
//  Using a SQL statement and a parameter array.
productDataSet = db.ExecuteDataSet(CommandType.Text,  " GetProductsByCategory ",
new Object[] {  " %bike% " });
//  Using a stored procedure and a parameter array.
productDataSet = db.ExecuteDataSet( " GetProductsByCategory ",
new Object[] {  " %bike% " });
//  Using a stored procedure and a named parameter.
DbCommand cmd = db.GetStoredProcCommand( " GetProductsByCategory ");
db.AddInParameter(cmd,  " CategoryID ", DbType.Int32,  7);
productDataSet = db.ExecuteDataSet(cmd);
复制代码
  

 

  将DataSet中的数据更新回数据库

  如果要更新数据库,可以使用Database的UpdateDataSet方法,返回受影响的行数总和。

  有一个参数是UpdateBehavior,决定了更新目标数据库表的方式。包括下面的几个值:

  Standard:如果更新失败,停止更新。

  Continue:如果更新失败,将继续其他的更新。

  Transactional:如果更新失败,所有的操作都将回滚。

  另外你还可以为UpdateDataSet方法的UpdateBatchSize参数提供一个值,设置更新方式为批量更新而不是一个一个的发送到数据库。这么做更高效,但是返回值就是最后一次批量发送的影响行数了,而不是所有的更新总数。

  加载数据到一个已经存在的DataSet  

 

复制代码
代码
<!--<br/ /> <br/ /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ /> http://www.CodeHighlighter.com/<br/ /> <br/ /> --> string selectSQL =  " SELECT Id, Name, Description FROM Products WHERE Id > 90 ";
//  Fill a DataSet from the Products table using the simple approach
DataSet simpleDS = defaultDB.ExecuteDataSet(CommandType.Text, selectSQL);
DisplayTableNames(simpleDS,  " ExecuteDataSet ");
//  Fill a DataSet from the Products table using the LoadDataSet method
//  This allows you to specify the name(s) for the table(s) in the DataSet
DataSet loadedDS =  new DataSet( " ProductsDataSet ");
defaultDB.LoadDataSet(CommandType.Text, selectSQL, loadedDS,
new  string[] {  " Products " });
DisplayTableNames(loadedDS,  " LoadDataSet ");
复制代码

 

  更新数据库UpdateDataSet

 

复制代码
代码
<!--<br/ /> <br/ /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ /> http://www.CodeHighlighter.com/<br/ /> <br/ /> --> string addSQL =  " INSERT INTO Products (Name, Description) VALUES (@name,
@description); " ;
string updateSQL =  " UPDATE Products SET Name = @name, Description = @description
WHERE Id = @id " ;
string deleteSQL =  " DELETE FROM Products WHERE Id = @id ";
//  Create the commands to update the original table in the database
DbCommand insertCommand = defaultDB.GetSqlStringCommand(addSQL);
defaultDB.AddInParameter(insertCommand,  " name ", DbType.String,  " Name ",
DataRowVersion.Current);
defaultDB.AddInParameter(insertCommand,  " description ", DbType.String,
" Description ", DataRowVersion.Current);
DbCommand updateCommand = defaultDB.GetSqlStringCommand(updateSQL);
defaultDB.AddInParameter(updateCommand,  " name ", DbType.String,  " Name ",
DataRowVersion.Current);
defaultDB.AddInParameter(updateCommand,  " description ", DbType.String,
" Description ", DataRowVersion.Current);
defaultDB.AddInParameter(updateCommand,  " id ", DbType.String,  " Id ",
DataRowVersion.Original);
DbCommand deleteCommand = defaultDB.GetSqlStringCommand(deleteSQL);
defaultDB.AddInParameter(deleteCommand,  " id ", DbType.Int32,  " Id ",
DataRowVersion.Original);
Finally, you can apply the changes by calling the UpdateDataSet method,  as shown here.
//  Apply the updates in the DataSet to the original table in the database
int rowsAffected = defaultDB.UpdateDataSet(loadedDS,  " Products ",
insertCommand, updateCommand, deleteCommand,
UpdateBehavior.Standard);
Console.WriteLine( " Updated a total of {0} rows in the database. ", rowsAffected);
复制代码

 

 

 

  以数据库连接为基础的事务操作

  就是操作一个数据库,以一个数据库连接为基础。

  一个常用的数据库解决方案,都会包括一些事务的处理,例如银行的转账。

  事务要满足ACID原则:

  Atomicity,原子性,所有的操作要么都成功,要么都失败。

  Consistency,一致性,数据在事务之前和之后保持数据的一致性。  

  Isolation,隔离性,在事务操作的过程中,其他操作不能访问和查看数据。

  Durability,一旦事务成功,在系统中产生的变化将是永久的。

  详细介绍参看:http://www.360doc.com/content/08/0426/18/61497_1217187.shtml

  

复制代码
代码
<!--<br/ /> <br/ /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ /> http://www.CodeHighlighter.com/<br/ /> <br/ /> --> using (DbConnection conn = db.CreateConnection())
{
conn.Open();
DbTransaction trans = conn.BeginTransaction();
try
{
//  execute commands, passing in the current transaction to each one
db.ExecuteNonQuery(cmdA, trans);
db.ExecuteNonQuery(cmdB, trans);
trans.Commit();  //  commit the transaction
}
catch
{
trans.Rollback();  //  rollback the transaction
}
}
复制代码
  

 

  分布式的事务

  如果你的事务包含了访问不同的数据库,或者是包括了消息队列的访问,你必须使用分布式的事务。例如windows的Distributed transaction coordinator(DTC)服务。

  使用TransactionScope类

  

<!--<br/ /> <br/ /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ /> http://www.CodeHighlighter.com/<br/ /> <br/ /> --> using (TransactionScope scope
new TransactionScope(TransactionScopeOption.RequiresNew))
{
//  perform data access here
}

 

  扩展模块使用其他类型的数据库

  企业库默认支持SQL Server、Oracle和SQL CE。但是你可以扩展来支持更多的数据库。自定义一个provider或者是引入第三方的provider。

  如果是自定义一个provider,你可以继承Database类,然后重写里面的一些方法。在方法中处理一些细节,例如返回值,参数的前缀(@),数据类型转换,和其他的一些相关因素。

转自:http://www.cnblogs.com/virusswb/archive/2010/05/16/Enterprise-Library-5-0-DataAccess-4.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值