. NET development in the transaction Department competition, enterprise-level service COM + transactions)

Source: Internet
Author: User

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.

CodeExample:

(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

Sample Code:

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;
}
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.