asp.net use instance code for transactions in a three-tier architecture-practical tips

Source: Internet
Author: User
Contact 3 layer also has a period of time, to understand the level of general, some time ago in the three layer how to use the business, where to put it? Sqlherper? The DAL? Bll?. Then I will be crazy Baidu, several times are not fruit (because do is small project, not the matter is not very related), today I check again, a good look at the csdn on the discussion, http://topic.csdn.net/u/20091101/19/ F21697d7-8f0c-4eb3-8e59-d0fe2f0b04b0.html, combined with the predecessors and masters of the opinions of their own changed one out. My idea is to write the transaction logic in the business logic layer, the processing of the database is also in the SQLHELPER,BLL layer through the transaction SqlTransaction pass value to access the DAL, and then access SqlHelper. The next step is to block the code.

Sqlhelper:
Copy Code code as follows:

private static SqlConnection Cnn = new SqlConnection ( dbconfig.connectionstring);
#region interpret SqlConnection to open the connection and open
///<summary>
///interpret SqlConnection to open the connection and open
///</summa Ry>
///<returns> return sqlconnection</returns>
private static SqlConnection getcnn ()
{
if (cnn.state = = connectionstate.closed)
{
Cnn.open ();
}
return to Cnn;
}
#endregion
#region Close database connection
///<summary>
///Close database connection
///</summary>
public static void Closecnn ()
{
Cnn.close ();
}
#endregion
#region generate a transaction and start
///<summary>
///generate a transaction and start
///</summary>
///<returns> return this transaction </returns>
public static SqlTransaction BeginTransaction ()
{
Sqltransa Ction tran = GETCNN (). BeginTransaction ();
Return Tran;
}
#endregion

DAL:
Copy Code code as follows:

public bool Test (int i,sqltransaction Tran)
{
String sql = "INSERT into [test] ([item]) VALUES (@i)";
Sqlparameter[] paras=new sqlparameter[]{new SqlParameter ("@i", I)};
Return SqlHelper. Executenquery (SQL, paras, CommandType.Text, Tran) >0;
}

Bll:
Copy Code code as follows:

Userdao Userdao = new Userdao ();
public bool Test ()
{
using (SqlTransaction tran = Sqlhelper.begintransaction ())
{
Try
{
Userdao.test (2, Tran);
Userdao.test (3, Tran);
Tran.commit (); return true;
}
Catch
{
Tran. Rollback ();
return false;
}
Finally
{
SQLHELPER.CLOSECNN ()//Close database connection
}
}
}

The above code in this test through, to be used in real projects, please modify and then use, there is my level generally, write the place please forgive me. You are welcome to guide me.
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.