C # database access through transactions

Source: Internet
Author: User

(C # transaction)

1. Create the transaction structure

Sqlconnection = new sqlconnection ();
... Initialize the connection

// Start the transaction
Sqltransaction =
Sqlconnection. begintransaction ();
// Apply the transaction to the command
Sqlcommand
Sqlcommand = new sqlcommand ();
Sqlcommand. Connection =
Sqlconnection;
Sqlcommand. Transaction = sqltransaction;

Try
{
// Use sqlcommand to perform data operations
...
// Submitted successfully

Sqltransaction. Commit ();
}
Catch (exception ex)
{

// Error rollback
Sqltransaction. rollback ();
}

2. Simple Example

{
Datatable dt = new
Datatable ();

System. Data. sqlclient. sqlconnection CNN = new
System. Data. sqlclient. sqlconnection ("connection string ");

System. Data. sqlclient. sqlcommand CM = new
System. Data. sqlclient. sqlcommand ();

Cm. Connection = CNN;
CNN. open ();

System. Data. sqlclient. sqltransaction trans =
CNN. begintransaction ();
Try

{
Foreach (datarow DR in
DT. Rows)

{
Cm. commandtext = "Update
[Table] Set [quantity] = @ amount where productid = @ productid ";


Cm. Parameters. Add ("@ amount", sqldbtype. INT );

Cm. Parameters ["@ amount"]. value = convert. toint32 (Dr ["amount"]);


Cm. Parameters. Add ("@ productid", sqldbtype. varchar );

Cm. Parameters ["@ productid"]. value =
Dr ["productid"]. tostring ();

Cm. executenonquery ();
}

Trans. Commit ();
}

Catch
{

Trans. rollback ();

}
Finally
{

CNN. Close ();

Trans. Dispose ();

CNN. Dispose ();
}
}

3. SQL Server transaction example

Begin transaction
Save transaction

Insert into demo values ('bb ',' B terminologies ')
Rollback
Transaction

Create Table demo2 (name varchar (10), age INT)
Insert
Demo2 (name, age) values ('Lis ', 1)
Rollback transaction

Insert into demo values ('bb', 'B
Term ')

Commit transaction

Commit transaction

4. Note

1. The transaction must begin intransaction () after the connection is opened ();

2. Add a transaction to sqlcommand (sqlcommand. Transaction =
Sqltransaction ;)

3. make corresponding adjustments for other databases

4. You can use a DLL provided by Microsoft, which is very convenient.

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.