// ================================================ ======================================
// Method 1: directly written to the SQL script
// Advantages: Combined with databases, high operation efficiency
// Disadvantage: it is restricted by the database. If you want to port data from sqlserver to another database, you may need to rewrite everything.
// ================================================ ======================================
Begin TRANS // start the transaction
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 // submit the transaction
Else
Rollback TRANS // roll back the transaction
// ================================================ ======================================
// Method 2: Implemented Using ado.net
// Advantage: no database restrictions,
// Disadvantage: it is a little difficult to implement cross-database operations on more than two databases for one thing.
// ================================================ ======================================
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. deleettings ["connectionstring"]);
Myconn. open ();
Sqltransaction mytrans = myconn. begintransaction ();
Sqlcommand mycommand = new sqlcommand ();
Mycommand. Connection = myconn;
Mycommand. Transaction = mytrans; // start the transaction
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 values (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 (); // submit the transaction
}
Catch (exception E)
{
Mytrans. rollback (); // roll back the transaction
Throw new exception (E. tostring ());
}
Finally
{
Mycommand. Dispose ();
Myconn. Close ();
Myconn. Dispose ();
}
// ================================================ ======================================
// Method 3: Use COM + transaction
// Advantage: the powerful transaction processing mechanism not only supports cross-database, but also supports load balancing.
// Disadvantage: the running efficiency is not as high as the above two types, and the advantages are troublesome during deployment.
// ================================================ ======================================
Using system. enterpriseservices;
Using system. runtime. compilerservices;
Using system. reflection;
// Supply the COM + application name.
[Assembly: applicationname ("complusexample")] // This COM + applicationProgramName
// Supply a strong-named assembly.
[Assembly: assemblykeyfileattribute ("complusexample. SNK")] // the strong-named file must be used.
Namespace Cl
{
[Transaction (transactionoption. Required)] // indicates that the class must support transactions.
Public class complusexample: servicedcomponent
{
[AutoComplete] // indicates automatic submission. If there is no exception in the hello () function, commit is triggered. If there is an exception, rollback is triggered.
Public String Hello ()
{
Return "COM + successful !!! ";
}
}
}
// ========================================== =======================================================< br> // remarks
// ============================================= =============================================================< br> 1. create a strong name
before compiling a component, you must specify a strong name for the component assembly. If this parameter is not specified, the COM + directory cannot recognize the component and cannot register it. In fact, you have specified a strong name through the assemblykeyfile attribute used previously. Now you need to use the strong name tool (sn.exe) to create a strong name and associate the guid with the Assembly.
the command prompt is displayed.
to create a strong name, enter the following Code in the command prompt and press Enter.
Sn-K complusexample. SNK
copy the complusexample. SNK file from the root directory of the hard drive (usually C:/) to the bin directory of the folder where the project is located.
now, you need to compile this program so that it can generate the files necessary to register this component on COM +. In Visual Studio. NET, click build on the build menu ).