Transferred from Li Tianping:
Http://www.cnblogs.com/ltp/archive/2009/06/17/1505318.html
COM +The transaction can be processed manually or automatically. The automatic processing method is added before the method to be automatically processed.[AutoComplete]To submit or roll back based on the normal method or thrown exception. Manual processing is calledContextutilClassEnablecommit,SetcompleteAndSetabortMethod.
The implementation steps are as follows.
1.ProgramAdd a strong name
1) Create a key pair
The tool used to create a key is calledSn.exe. You can run the command to generate and extract keys. We need to use the following method to runSn.exe. Sn-k c: \ key. SNK
WhereKey. SNK
Indicates the name of the file that will save the key. Its name can be arbitrary, but it is used to contain. SNKSuffix.
2) Signature
This file must be inAssemblykeyfileAttribute reference. The signature is usually implemented during compilation. Signature time,Available to UsersC# Attribute notification compiler should use the correct key file pairDLL. To do this, you need to openAssemblyinfo. CSFile and modify it.
[Assembly: assemblykeyfile (".. \ key. SNK")]
Note |
Key. SNKFiles and project files are in the same folder. |
2 . Manual Transaction Processing
Create a project business class to implement Transaction ProcessingClasstran.
(Sample location: CD \Code \ ch05 \ 04 \ classtran \ orderdata1)
Using System;
Using System. Data. sqlclient;
Using System. incluiseservices;
// Enterprise-level service COM + transactions
Namespace Classtran
{
[Transaction (transactionoption. Required)]
Public Class Orderdata1: servicedcomponent
{
// Manual transactions
Public String Worktran ()
{
Try
{
Contextutil. enablecommit ();
Work1 ();
Work2 ();
Contextutil. setcomplete ();
Return
" Successful! " ;
}
Catch (Exception ex)
{
Contextutil. setabort ();
Return
" Failed! " ;
}
}
Private Void Work1 ()
{
String Constring = " Data
Source = 127.0 . 0.1 ; Database = codematic;
Userid = sa; Password = " ;
Sqlconnection myconnection = New Sqlconnection (constring );
String Strsql = " Insert P_category (categoryid, name) values ( ' 1 ' , ' Test1 ' ) " ;
Sqlcommand mycommand = New
Sqlcommand (strsql, myconnection );
Myconnection. open ();
Int Rows = mycommand. executenonquery ();
Myconnection. Close ();
}
Private Void Work2 ()
{
String Constring = " Data
Source = 127.0 .0.1 ; Database = codematic;
Userid = sa; Password = " ;
Sqlconnection myconnection = New Sqlconnection (constring );
String Strsql = " Insert P_category (categoryid, name) values ( ' 2 ' , ' Test2 ' ) " ;
Sqlcommand mycommand = New
Sqlcommand (strsql, myconnection );
Myconnection. open ();
Int Rows = mycommand. executenonquery ();
Myconnection. Close ();
}
}
}
3. Automatic Transaction Processing
Add attributes BEFORE THE METHOD[AutoComplete (true)]In this way, if there is no exception during method execution, it will be submitted by default. If there is an exception, this method will be rolled back.
(Sample location: CD \Code \ ch05 \ 04 \ classtran \ orderdata2)
Using System;
Using System. Data. sqlclient;
Using System. incluiseservices; // Enterprise-level service COM + transactions
Namespace Classtran
{
[Transaction (transactionoption. Required)]
Public Class Orderdata2: servicedcomponent
{
// Automatic transactions
[AutoComplete ( True )]
Public String Worktran ()
{
String MSG = "" ;
String Constring = " Data
Source = 127.0 . 0.1 ; Database = codematic;
User ID = sa; Password = " ;
Sqlconnection myconnection = New Sqlconnection (constring );
Myconnection. open ();
Sqlcommand mycommand = New
Sqlcommand ();
Mycommand. Connection = myconnection;
Try
{
Mycommand. commandtext = " Update p_product set name = 'computer 2' Where Id = 52 " ;
Mycommand. executenonquery ();
Mycommand. commandtext = " Update p_product set name = 'computer 3' Where Id = 53 " ;
Mycommand. executenonquery ();
MSG = " Successful! " ;
}
Catch (Exception ex)
{
MSG = " Failed: " + Ex. message;
}
Finally
{
Myconnection. Close ();
}
Return
MSG;
}
}
}