A Transaction is a unit of concurrency control and a sequence of user-defined operations. These operations are either done or not done. They are an inseparable unit of work.
Through transactions, SQL Server can bind a set of logical operations to ensure data integrity on the Server.
In the SQL server +. net development environment, there are two ways to complete transaction operations to maintain the data integrity of the database;
One is SQL stored procedures, and the other is a simple transaction processing in ADO. NET;
Now we use a typical bank transfer example to illustrate the usage of these two examples.
Let's take a look at how SQL stored procedures perform transaction operations:
First, create a table:
Create database aaaa -- create a table that contains the user's account and money
go
use aaaa
create table bb
(
ID int not null primary key, -- account
Moneys money -- transfer amount
)
Insert into bb values ('1', '20140901') -- insert two pieces of data
insert into bb values ('2','3000')
Use this table to create a stored procedure:
Create procedure mon -- create a stored procedure and define SEVERAL VARIABLES
@ ToID int, -- the account that receives the transfer
@ FromID int, -- transfer out your account
@ Momeys money -- transfer amount
As
Begin tran -- start transaction execution
Update bb set moneys = moneys-@ momeys where ID = @ fromID-the first operation to be executed. The money is transferred, minus the amount transferred.
Update bb set moneys = moneys + @ momeys where ID = @ toID -- perform the second operation, accept the transfer amount, and increase
If @ error <> 0 -- determines if either of the two statements has an error
Begin rollback tran-start to execute the transaction rollback, before the recovery transfer starts
Return 0
End
Go
Else -- how to successfully execute both
Begin commit tran
Return 1
End
Go
Next let's take a look at how C #. net calls this stored procedure:
Protected void button#click (object sender, EventArgs e)
{
SqlConnection con = new SqlConnection (@ "Data Source =. \ SQLEXPRESS; database = aaaa; uid = sa; pwd = jcx"); // connection string
Sqlcommand cmd = new sqlcommand ("mon", con); // call the Stored Procedure
Cmd. commandtype = commandtype. storedprocedure;
Con. open ();
Sqlparameter prar = new sqlparameter (); // pass the Parameter
Cmd. Parameters. addwithvalue ("@ fromid", 1 );
Cmd. Parameters. addwithvalue ("@ toid", 2 );
Cmd. Parameters. addwithvalue ("@ momeys", convert. toint32 (textbox1.text ));
Cmd. Parameters. Add ("@ return", ""). Direction = parameterdirection. returnvalue; // obtain the return value of the stored procedure
Cmd. executenonquery ();
String value = cmd. Parameters ["@ return"]. Value. ToString (); // value the returned value to the Value.
If (value = "1 ")
{
Label1.Text = "added successfully ";
}
Else
{
Label1.Text = "failed to add ";
}
}
This is to add a transaction to the stored procedure. Let's take a look at how ADO. NET handles the transaction instead of writing the SQL stored procedure to the database:
Protected void Button2_Click (object sender, EventArgs e)
{
SqlConnection con = new SqlConnection (@ "Data Source =. \ SQLEXPRESS; database = aaaa; uid = sa; pwd = jcx ");
Con. Open ();
SqlTransaction tran = con. BeginTransaction (); // first instance the SqlTransaction class, which uses the con
This connection uses the BeginTransaction method to start executing this transaction.
SqlCommand cmd = new SqlCommand ();
Cmd. Connection = con;
Cmd. Transaction = tran;
Try
{
// At try {}
Block to execute the sqlcommand command,
Cmd. CommandText = "update bb set moneys = moneys-'" + Convert. ToInt32 (TextBox1.Text) + "'where ID = '1 '";
Cmd. ExecuteNonQuery ();
Cmd. CommandText = "update bb set moneys = moneys + 'A' where ID = '2 '";
Cmd. ExecuteNonQuery ();
Tran. Commit (); // if both SQL commands are successfully executed, execute the commit method to perform these operations.
Label1.Text = "added successfully ";
}
Catch
{
Label1.Text = "failed to add ";
Tran. Rollback (); // if an exception occurs, execute the rollback method and roll back to the beginning of the transaction operation;
}
}
This is a simple example of different transaction usage, ADO. NET
The transaction processing method seems simple, but he needs to use the same connection to execute these operations. If he wants to use several databases to execute these operations with one transaction at the same time, it will be complicated, however, if you use SQL stored procedures, this is relatively simple. In short, the two methods have their own advantages.