首頁 資料庫 mysql教程 TransactionScope和Enterprise Libray 3.0 Data Access Applicat

TransactionScope和Enterprise Libray 3.0 Data Access Applicat

Jun 07, 2016 pm 03:44 PM
enterprise li transactionscope

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.
/// true if sucessful; otherwise false.
/// Demonstrates executing multiple updates within the
/// 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

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

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

熱門話題

Java教學
1669
14
CakePHP 教程
1428
52
Laravel 教程
1329
25
PHP教程
1273
29
C# 教程
1256
24
li是什麼元素 li是什麼元素 Aug 03, 2023 am 11:19 AM

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

enterprise是什麼版本 enterprise是什麼版本 Jan 12, 2021 am 10:55 AM

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

html中li是什麼 html中li是什麼 Nov 19, 2021 pm 03:31 PM

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

css怎麼去掉li預設樣式 css怎麼去掉li預設樣式 Jan 28, 2023 pm 02:09 PM

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

transactionscope的使用方法 transactionscope的使用方法 Dec 15, 2023 am 11:30 AM

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

怎麼使用transactionscope 怎麼使用transactionscope Dec 15, 2023 pm 02:37 PM

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

微軟公佈 Bing Chat 和 Enterprise 版更名為 Copilot 微軟公佈 Bing Chat 和 Enterprise 版更名為 Copilot Nov 18, 2023 pm 02:17 PM

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

transactionscope怎麼使用 transactionscope怎麼使用 Oct 07, 2023 pm 03:41 PM

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

See all articles