Use of Asp.net transactions in a three-tier architecture

Source: Internet
Author: User

I know more. net, there are two ways to execute transactions. One is automatic transactions, which is quite simple to use, but you need to configure the server. If you are using a web shared host, that is to say, if you do not have the permission to configure your own server, you do not need to use it. Next let's talk about it. the most common transaction SqlTransaction in. net.
I have been in touch with Layer 3 for a while. I know the level is average. I was wondering how to use transactions in Layer 3 some time ago. Where should I put them? Sqlherper? DAL? BLL ?. Then I went crazy About Baidu, which had failed for several times (because it was a small project and had little to do without transactions). I made a new one based on the opinions of my predecessors and masters. My idea is to write the transaction logic in the business logic layer. The database processing is still in SQLHELPER, and The BLL layer accesses the DAL through the SqlTransaction passed value, and then Sqlhelper. Next is the code of the chunks.
 
Sqlhelper:
1 private static SqlConnection Cnn = new SqlConnection (DbConfig. ConnectionString );
2
3 # Check whether SqlConnection is enabled and enabled by region
4 /// <summary>
5 // determine whether to enable and enable SqlConnection
6 /// </summary>
7 // <returns> return SqlConnection </returns>
8 private static SqlConnection GetCnn ()
9 {
10 if (Cnn. State = ConnectionState. Closed)
11 {
12 Cnn. Open ();
13}
14 return Cnn;
15}
16 # endregion
17
18 # region close database connection
19 /// <summary>
20 // close the database connection
21 /// </summary>
22 public static void CloseCnn ()
23 {
24 Cnn. Close ();
25}
26 # endregion
27
28 # region generates a transaction and starts
29 /// <summary>
30 /// generate a transaction and start
31 /// </summary>
32 // <returns> return this transaction </returns>
33 public static SqlTransaction BeginTransaction ()
34 {
35 SqlTransaction tran = GetCnn (). BeginTransaction ();
36 return tran;
37}
38 # endregion


 
DAL:
1 public bool test (int I, SqlTransaction tran)
2 {
3 string SQL = "insert into [test] ([item]) values (@ I )";
4 SqlParameter [] paras = new SqlParameter [] {new SqlParameter ("@ I", I )};
5 return sqlhelper. ExecutenQuery (SQL, paras, CommandType. Text, tran)> 0;
6}

BLL:
1 UserDAO userdao = new UserDAO ();
2
3
4 public bool test ()
5 {
6 using (SqlTransaction tran = SQLHelper. BeginTransaction ())
7 {
8 try
9 {
10 userdao. test (2, tran );
11 userdao. test (3, tran );
12 tran. Commit (); return true;
13}
14 catch
15 {
16
17 tran. Rollback ();
18 return false;
19}
20 finally
21 {
22 SQLHelper. CloseCnn (); // close the database connection
23}
24}
25}

Www.2cto.com


The above code is passed in this test. If you want to use it in a real project, Please modify the code before using it. The level is average. If you cannot write it, please forgive me. Thank you for your guidance.

 

From breeze 333

Related Article

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.