[轉貼]在Asp.Net中的幾種交易處理的方法

來源:互聯網
上載者:User

//======================================================================
//方法1:直接寫入到Sql指令碼中
//優點:和資料庫結合,運行效率高
//缺點:受到資料庫的約束,如果要從sqlserver移植到其他資料庫,可能要重寫所有事物
//======================================================================
begin trans//開始事務
declare @orderDetailsError int , @productError ing
delete from "order details" where productId = 42
select @orderDetailsError = @@ERROR
delete from Products where productId = 42
select @productError = 0 and @productError = 0
commit trans//提交事務
else
rollback trans//復原事務

 

//======================================================================
//方法2:使用Ado.net實現
//優點:不受資料庫限制,
//缺點:對於一個事物要操作兩個以上的資料庫的跨資料庫的操作的實現有一點困難。
//======================================================================
public void Exec_Jiaoyi( int  customerId,string  customer,int  lastNum,int  receiptId,string  seller,string  principal,DateTime  bargainTime , decimal addMoney , string memo )
{
SqlConnection myConn = new SqlConnection( System.Configuration.ConfigurationSettings.AppSettings["connectionString"] ) ;
myConn.Open ( ) ;
SqlTransaction myTrans = myConn.BeginTransaction ( ) ;
SqlCommand myCommand=new SqlCommand ( );
myCommand.Connection = myConn ;
myCommand.Transaction = myTrans ;//開始事務
try
{
myCommand.Parameters.Add(new SqlParameter("@m_customerId", SqlDbType.Int,0)).Value=customerId;
myCommand.Parameters.Add(new SqlParameter("@m_customer", SqlDbType.NVarChar,100)).Value=customer;
myCommand.Parameters.Add(new SqlParameter("@m_lastNum", SqlDbType.Int,0)).Value=lastNum;
myCommand.Parameters.Add(new SqlParameter("@m_receiptId", SqlDbType.Int,0)).Value=receiptId;
myCommand.Parameters.Add(new SqlParameter("@m_seller", SqlDbType.NVarChar,100)).Value=seller;
myCommand.Parameters.Add(new SqlParameter("@m_principal", SqlDbType.NVarChar,100)).Value=principal;
myCommand.Parameters.Add(new SqlParameter("@m_bargainTime", SqlDbType.DateTime,0)).Value=bargainTime;
myCommand.Parameters.Add(new SqlParameter("@m_addMoney", SqlDbType.Decimal,9)).Value=addMoney;
myCommand.Parameters.Add(new SqlParameter("@m_memo", SqlDbType.NVarChar,100)).Value=memo;
SqlParameter returnParam = myCommand.Parameters.Add(new SqlParameter("@thisId",SqlDbType.Int));
returnParam.Direction    = ParameterDirection.Output;

//SELECT @thisId=SCOPE_IDENTITY() FROM caiwzhk
//SqlParameter returnParam = myCommand.Parameters.Add(new SqlParameter("@thisId",SqlDbType.Int));
//returnParam.Direction    = ParameterDirection.Output;
//returnId = (int)myCommand.Parameters["@thisId"].Value;
myCommand.CommandText = "insert into jiaoyxx_pack ( customerId,customer,lastNum,receiptId,seller,principal,bargainTime) values (@m_customerId,@m_customer,@m_lastNum,@m_receiptId,@m_seller,@m_principal,@m_bargainTime) SELECT @thisId=SCOPE_IDENTITY() FROM jiaoyxx_pack" ;
myCommand.ExecuteNonQuery ( ) ;

int returnId = (int)myCommand.Parameters["@thisId"].Value;
myCommand.CommandText = "insert into chongzhgl_pack ( bargainId,customerId,customer,addMoney,addTime,principal,memo) values ( " + returnId.ToString() + " ,@m_customerId,@m_customer,@m_addMoney,@m_bargainTime,@m_principal,@m_memo)" ;
myCommand.ExecuteNonQuery ( ) ;

myTrans.Commit ( ) ;//提交事務
}
catch ( Exception e )
{
myTrans.Rollback ( ) ;//復原事務
throw new Exception ( e.ToString ( ) ) ;
}
finally
{
myCommand.Dispose();
myConn.Close ( ) ;
myConn.Dispose();
}

 

//======================================================================
//方法3:使用COM+事務
//優點:強大的事物處理機制,不但支援跨資料庫,還支援負載平橫等。
//缺點:運行效率不如上面兩種,部署的時候優點麻煩
//======================================================================
using System.EnterpriseServices;
using System.Runtime.CompilerServices;
using System.Reflection;

// Supply the COM+ application name.
[assembly: ApplicationName("ComPlusExample")]//這個COM+應用程式的名稱
// Supply a strong-named assembly.
[assembly: AssemblyKeyFileAttribute("ComPlusExample.snk")]//一定要strong-named檔案

namespace cl
{
[Transaction(TransactionOption.Required)]//表示類是要支援事務的
public class ComPlusExample : ServicedComponent
{
[AutoComplete] //表示自動認可,hello()函數沒有異常就commit,有異常就rollback
public string hello()
{
return "com+成功!!!"; 
}
}
}

 

//======================================================================
//備忘
//======================================================================
1.建立強式名稱
  在編譯組件之前,您需要為此組件的程式集指定一個強式名稱。如果不指定,COM+ 目錄將不能識別該組件,也就無法註冊它。實際上,您已經通過前面使用的 AssemblyKeyFile 屬性指定了強式名稱,現在需要使用強式名稱工具 (Sn.exe) 建立強式名稱並使 GUID 與程式集關聯。
開啟命令提示。
要建立強式名稱,請在命令提示下鍵入以下代碼,然後按 Enter 鍵。
sn -k ComPlusExample.snk
將 ComPlusExample.snk 檔案從硬碟的根目錄(通常為 C:/)複製到項目所在檔案夾的 bin 目錄下。
  現在,需要編譯此程式,使它能產生在 COM+ 註冊此組件必需的檔案。在 Visual Studio .NET 中,在 Build(產生)菜單上,單擊 Build(產生)。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.