ホームページ データベース 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 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

エンタープライズとはどのバージョンですか? エンタープライズとはどのバージョンですか? Jan 12, 2021 am 10:55 AM

enterprise は、Windows システムのエンタープライズ バージョンです。Windows エンタープライズ バージョンは、主に大企業および中規模企業を対象としており、Direct Access、Windows To Go Creator、AppLokcer、BranchCache などのさまざまな実用的な機能が追加されています。

李って何の元素? 李って何の元素? Aug 03, 2023 am 11:19 AM

li は HTML マークアップ言語の要素であり、リストの作成に使用されます。 li は、ul または ol の子要素であるリスト項目を表します。li タグの役割は、リスト内の各項目を定義することです。 HTML では、通常、li 要素は、順序付きリストまたは順序なしリストを作成するために ul 要素または ol 要素とともに使用されます。順序なしリストは ul 要素を使用し、リスト項目は li 要素で表されますが、順序付きリストは ol 要素を使用し、また、 li 要素表現。

HTMLのliとは何ですか HTMLのliとは何ですか Nov 19, 2021 pm 03:31 PM

HTML における li の正式な英語名は「list item」で、「リスト項目」を意味します。リスト項目を定義する要素タグです。構文は「<li>list item content</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 デフォルト スタイルを削除します。

トランザクションスコープの使い方 トランザクションスコープの使い方 Dec 15, 2023 am 11:30 AM

transactionscope の使用方法: 1. ネームスペースを導入する; 2. TransactionScope オブジェクトを作成する; 3. トランザクションを開始する; 4. データベース操作を実行する; 5. トランザクションを送信またはロールバックする。詳細な導入: 1. 名前空間を導入します。TransactionScope を使用する前に、System.Transactions 名前空間を導入する必要があります。2. TransactionScope を使用する場合は、TransactionScope オブジェクトを作成します。

トランザクションスコープの使い方 トランザクションスコープの使い方 Dec 15, 2023 pm 02:37 PM

transactionscope を使用する手順: 1. ネームスペースを導入する; 2. TransactionScope オブジェクトを作成する; 3. トランザクションを開始する; 4. データベース操作を実行する; 5. トランザクションを送信またはロールバックする。詳細な導入: 1. 名前空間を導入します。TransactionScope を使用する前に、System.Transactions 名前空間を導入する必要があります。2. トランザクションを使用する必要があるコード ブロック内に TransactionScope オブジェクトを作成します。

Microsoft、Bing Chat および Enterprise エディションの Copilot へのブランド変更を発表 Microsoft、Bing Chat および Enterprise エディションの Copilot へのブランド変更を発表 Nov 18, 2023 pm 02:17 PM

11 月 16 日のこのサイトのニュースによると、本日の Microsoft Ignite 2023 開発者カンファレンスで、Microsoft は BingChat とそのエンタープライズ プレミアム バージョン BingChat for Enterprise の名前が正式に Copilot! に変更されたと発表しました。 Microsoft コミュニケーション ディレクターの Caitlin Roulston 氏は、同社は「BingChat Enterprise」の名前を「Copilot」に変更することを決定したと述べ、この変更は、消費者と企業顧客向けに統合された Copilot エクスペリエンスを作成するという Microsoft のビジョンを反映していると述べました。もちろん、変更されたのは名前だけではありません。 12 月 1 日以降、Enterprise アカウント (Microsoft

CSS liで異なる色を表示する方法 CSS liで異なる色を表示する方法 Jan 28, 2023 pm 01:48 PM

CSS li にさまざまな色を実装する方法: 1. "ul li::marker {color: #3860f4;}" 属性を使用して li の色を変更します; 2. "li:before {content: "";width: 6px; を使用して li の色を変更します。 height : 6px;display: inline-block;border-radius: 50%;background: #4F8EFF...」属性で色を設定します。

See all articles