Several methods for processing transactions in ASP. NET

Source: Internet
Author: User

1. stored in the SQL Server database
Create proc registeruser
(@ Usrname varchar (30), @ usrpasswd varchar (30), @ age int, @ phonenum varchar (20), @ address varchar (50 ))
As
Begin
// Display the definition and start a transaction
Begin tran
Insert into user (username, userpasswd) values (@ usrname, @ usrpasswd)
If @ error <> 0
Begin
// If the operation fails, the transaction is rolled back.
Rollback tran
// Return the stored procedure and set the return code as a transaction operation failure
Return-1
End
Insert into userdoc (username, age, sex, phonenumber, address)
Values (@ usrname, @ age, @ phonenum, @ address)
If @ error <> 0
Begin
// If the operation fails, the transaction is rolled back.
Rollback tran
Return-1
End
// If the operation is correct, the transaction is committed.
Commit tran
Return 0
End
2. Use the begintransaction method of the connection object in the C # code to create a transaction.
Try
{
Sqlconnection con = new sqlconnection ();
Sqltransaction TRAN = con. begintransaction ();
String export text1 = "delete ...";
Sqlcommand cmd1 = new sqlcommand (CMD, con );
Statement 1.executenonquery ();
String plain text = "insert ...";
Sqlcommand cmd2 = new sqlcommand (cmd2, con );
Listen 2.executenonquery ();
Tran. Commit ();
}
Catch (exception ex)
{
Tran. rollback ();
Throw ex;
}
3. Use the transaction object parameter of the excutenonquery method of the database object in the enterprise database.
// Add data from two tables using transactions
Using (dbconnection dbcon = dB. createconnection ())
{
Dbcon. open ();
Dbtransaction dbtran = dbcon. begintransaction ();
Try
{

// Add a meeting first
DB. executenonquery (dbtran, "addmeeting",-1, subject, partition SOR, meetingroom, date, starttime, endtime,
Manager, actor, agenda, isapproved, ispublished );

String [] attvalue = stratt. Split (';');
Int rowscount = 0;
For (INT I = 0; I <attvalue. length; I ++)
{
// Construct the attachment data
String [] ATT = attvalue [I]. Split (',');
String filename = ATT [0];
String fileext = ATT [1];
String filepath = ATT [2]. Replace ("/","\\");
String note = ATT [3];
// Additional purpose 1 indicates the meeting material type
Int usage = 1;
Long filesize = convert. toint64 (ATT [4]);
Datetime uptime = convert. todatetime (ATT [5]);
// Add an attachment
Rowscount = dB. executenonquery (dbtran, "addattachment",-1, recid, filename, fileext, filepath, note, usage, filesize, uptime );
}
Dbtran. Commit ();
}
Catch (exception ex)
{
Dbtran. rollback ();
}
Finally
{
Dbcon. Close ();
}

 

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.