天天看點

在Asp.Net中的幾種事務處理的方法

//======================================================================

//方法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(生成)。