TransactionScope和Enterprise Libray 3.0 Data Access Applicat
Enterprise Libray 3.0已经发布了,具体可参见TerryLee的 Enterprise Library 3.0 发布.下载了看看,有非常激动人心的更新.我只是看看Data Access Application Block代码,代码中有这个类TransactionScopeConnections,是个内部类,设计意图很明显就是使用数据库
Enterprise Libray 3.0已经发布了,具体可参见TerryLee的 Enterprise Library 3.0 发布.下载了看看,有非常激动人心的更新.我只是看看Data Access Application Block代码,代码中有这个类TransactionScopeConnections,是个内部类,设计意图很明显就是使用数据库的事务模型.我觉得设计为内部类有点瑕疵,我的习惯是事务和提交在业务逻辑层. .NET 2.0的System.Transactions应该是一个更好的选择。就将Data Access Application Block的QuickStart例子代码:
///
/// Transfers an amount between two accounts.
///
/// Amount to transfer.
/// Account to be credited.
/// Account to be debited.
///
///
/// context of a transaction.
public bool Transfer(int transactionAmount, int sourceAccount, int destinationAccount)
{
bool result = false;
// Create the Database object, using the default database service. The
// default database service is determined through configuration.
Database db = DatabaseFactory.CreateDatabase();
// Two operations, one to credit an account, and one to debit another
// account.
string sqlCommand = "CreditAccount"
DbCommand creditCommand = db.GetStoredProcCommand(sqlCommand);
db.AddInParameter(creditCommand, "AccountID", DbType.Int32, sourceAccount);
db.AddInParameter(creditCommand, "Amount", DbType.Int32, transactionAmount);
sqlCommand = "DebitAccount"
DbCommand debitCommand = db.GetStoredProcCommand(sqlCommand);
db.AddInParameter(debitCommand, "AccountID", DbType.Int32, destinationAccount);
db.AddInParameter(debitCommand, "Amount", DbType.Int32, transactionAmount);
using (DbConnection connection = db.CreateConnection())
{
connection.Open();
DbTransaction transaction = connection.BeginTransaction();
try
{
// Credit the first account
db.ExecuteNonQuery(creditCommand, transaction);
// Debit the second account
db.ExecuteNonQuery(debitCommand, transaction);
// Commit the transaction
transaction.Commit();
result = true;
}
catch
{
// Rollback transaction
transaction.Rollback();
}
connection.Close();
return result;
}
}
按照TransactionScope类进行改造,试验成功了,代码如下:
public bool Transfer(int transactionAmount, int sourceAccount, int destinationAccount)
{
bool result = false;
Database database = DatabaseFactory.CreateDatabase();
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
TestCommand1(database, transactionAmount, sourceAccount);
TestCommand2(database, transactionAmount, destinationAccount);
scope.Complete();
result = true;
}
return result;
}
private void TestCommand1(Database db, int transactionAmount, int sourceAccount)
{
string sqlCommand = "CreditAccount"
DbCommand creditCommand = db.GetStoredProcCommand(sqlCommand);
db.AddInParameter(creditCommand, "AccountID", DbType.Int32, sourceAccount);
db.AddInParameter(creditCommand, "Amount", DbType.Int32, transactionAmount);
// Credit the first account
db.ExecuteNonQuery(creditCommand);
}
private void TestCommand2(Database db, int transactionAmount, int destinationAccount)
{
string sqlCommand = "DebitAccount"
DbCommand debitCommand = db.GetStoredProcCommand(sqlCommand);
db.AddInParameter(debitCommand, "AccountID", DbType.Int32, destinationAccount);
db.AddInParameter(debitCommand, "Amount", DbType.Int32, transactionAmount);
// Debit the second account
db.ExecuteNonQuery(debitCommand);
}
DAAB 在一个事务中可以在一个数据库连接中检测到几个命令的执行,这样可以避免虽然一个数据库连接执行的几个命令而启用 分布式事务 。在企业类库2.0的DAAB常常启用了分布式事务,就凭这一点,使用企业类库2.0的同学们有必要升级到企业类库3.0。
Parameter Discovery on Ms Access and SqlServer. using Microsoft Patterns and Practices DataBlock version 3.0 final
http://www.codeproject.com/useritems/Parameter_DiscoveryV292.asp

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

li是HTML標記語言中的元素,用於建立清單。 li代表列表項,它是ul或ol的子元素,li標籤的作用是定義列表中的每個項目。在HTML中,li元素通常與ul或ol元素配合使用來建立有序或無序列表,無序列表使用ul元素,列表項以li元素表示,而有序列表則使用ol元素,同樣也用li元素表示。

enterprise是Windows系統企業版,Windows企業版主要針對中型企業,其加入了Direct Access、Windows To Go Creator、AppLokcer、BranchCache等多種實用功能。

在html中,li的英文全稱為“list item”,意思為“清單項目”,是定義清單項目的元素標籤,語法“<li>清單項目內容</li>”;“<li>”標籤可用在有序列表“<ol>”和無序列表“<ul>”中。

css去掉li預設樣式的方法:1.建立一個HTML範例檔;2、新增li標籤內容;3、在css中透過將「list-style-type」屬性設為「none」即可去掉li預設樣式。

transactionscope的使用方法:1、引入命名空間;2、建立TransactionScope物件;3、開始事務;4、執行資料庫操作;5、提交或回溯事務。詳細介紹:1、引入命名空間,在使用TransactionScope之前,需要引入System.Transactions命名空間;2、建立TransactionScope對象,使用TransactionScope時等等。

使用transactionscope的步驟:1、引入命名空間;2、建立TransactionScope物件;3、開始事務;4、執行資料庫操作;5、提交或回溯事務。詳細介紹:1、引入命名空間,在使用TransactionScope之前,需要先引入System.Transactions命名空間;2、建立TransactionScope對象,在需要使用事務的程式碼區塊中等等。

本站11月16日消息,在今天的MicrosoftIgnite2023開發者大會上,微軟宣布BingChat及其企業高級版BingChatforEnterprise正式更名為Copilot!微軟通訊總監CaitlinRoulston表示,公司決定將"BingChatEnterprise"更名為"Copilot",這一改動體現了微軟為消費者和商業客戶打造統一的Copilot體驗的願景當然,不僅僅是名字變了。從12月1日起,使用企業帳戶(確切地說是Microso

TransactionScope是.NET Framework中用於管理事務的類,提供了簡單而靈活的方式來處理事務,確保一組相關的操作要么全部成功執行,要么全部回滾。透過使用TransactionScope,可以保持資料的一致性,提高應用程式的可靠性和穩定性。
