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