C # implements AOP transactions with attributes [C # | AOP | Attribute | ContextAttribute | Icontributeobjectsink | IMessageSink]
Notice before reading
1. The core of the entire article and the breakthrough point is the use of context contexts, it is important to pay attention to the role of CallContext throughout the program
2. The sqlhelper used in this article are Microsoft SqlHelper.cs.
3. This article focuses on how to achieve, and has been tested through, only paste key code, so please read carefully, some of the code directly copied down to run will be wrong!
Body
First, let's look at a piece of code that does not add to the transaction:
SqlDAL.cs
Public abstract class Sqldal {#region ConnectionString private sqlconnectionstringbuilder _connectio
nstring = null; <summary>///String Connection///</summary> public virtual SqlConnectionStringBuilder Conne ctionstring {get {if (_connectionstring = = NULL | | string. IsNullOrEmpty (_connectionstring.connectionstring)) {_connectionstring = new Sqlconnec
Tionstringbuilder (configurations.sqlserver_connection_string);
return _connectionstring;
set {_connectionstring = value;}
#endregion #region executenonquery public int ExecuteNonQuery (string cmdtext) {
Return Sqlhelper.executenonquery (connectionstring.connectionstring, CommandType.Text, Cmdtext); public int ExecuteNonQuery (string cmdtext, CommandType type) {
Return Sqlhelper.executenonquery (connectionstring.connectionstring, type, cmdtext);
public int ExecuteNonQuery (string cmdtext, CommandType type, params sqlparameter[] cmdparameters) {
Return Sqlhelper.executenonquery (connectionstring.connectionstring, type, Cmdtext, cmdparameters); } #endregion