asp.net Execute transaction code _ Practical Tips
Last Update:2017-01-19
Source: Internet
Author: User
Database Execution Area,
SqlConnection _sqlconn = null;
SqlTransaction _sqltrans = null;
Try
{
Database Operations Class
_sqlconn = System.mySQLConnection.SqlConnections.GetSQLConnection ()//See the following class
_sqlconn.open ();
SQL Transaction Object
_sqltrans = _sqlconn.begintransaction ();
Basic information Execute SQL statement case
BOOL Studentinfo_yn = false;
if (Hid_studentidvalue = "")
{
Studentinfo_yn = (System.mySQLConnection.SqlConnections.UpdateOrDelete (_sqlconn, _sqltrans, SQL statements) > 0)? True:false;
}
Else
{
Studentinfo_yn = true;
}
BOOL Baoming_yn = false;
Register basic information Execute SQL statement case
if (Studentinfo_yn)
{
Gets the O_FLOWID number of the registration, which is generated by the database
Baoming_yn = (System.mySQLConnection.SqlConnections.UpdateOrDelete (_sqlconn, _sqltrans, SQL statements) > 0)? True:false;
}
Else
{
Baoming_yn = false;
}
BOOL Baomingdetail_yn = false;
The course details of the application execute the SQL statement case
if (Baoming_yn)
{
Baomingdetail_yn = (System.mySQLConnection.SqlConnections.UpdateOrDelete (_sqlconn, _sqltrans, SQL statements) > 0)? True:false;
}
Else
{
Baomingdetail_yn = false;
}
Billing Table SQL statement case
if (Baoming_yn)
//{
Baomingdetail_yn = (System.mySQLConnection.SqlConnections.UpdateOrDelete (_sqlconn, _sqltrans, SQL statements) > 0)? True:false;
//}
Else
//{
Baomingdetail_yn = false;
//}
Total judgment is released successfully.
if (Baomingdetail_yn)
{
Commit TRANSACTION, clear session verification code
_sqltrans.commit ();
Messagebox.alert (Page, "registered successfully.") "," window.location.href= ' addbaoming.aspx ' ");
}
Else
{
_sqltrans.rollback ();
Messagebox.alert (Page, "enrollment failed.") ");
Return
}
}
catch (SqlException Sqle)
{
Messagebox.alert (Page, "submit Exception" + sqlE.Message.Replace ("\", "'"));
}
Finally
{
if (_sqlconn!= null)
_sqlconn.close ();
if (_sqltrans!= null)
_sqltrans.dispose ();
}
Written in sqlconnections classes (typically in App_Code or in classes with three-tier schemas)
The current connection is the first few.
static int count = 0;
Maximum Connection Amount
static int maxcount = 30;
Static sqlconnection[] Sqlconns = new Sqlconnection[maxcount];
Create your own
static Sqlconnections myconn = new Sqlconnections ();
Class
Private Sqlconnections ()
{
for (int i = 0; i < Maxcount; i++)
{
Sqlconns[i] = new SqlConnection (system.configuration.configurationsettings.appsettings["ADOStr"). ToString ());
}
}
Getting a database connection
public static int Updateordelete (String sqltxt)
{
SqlConnection sqlconntemp = Sqlconnections.getsqlconnection ();
Try
{
SqlCommand sqlcommt = new SqlCommand (Sqltxt, sqlconntemp);
Sqlconntemp.open ();
return Sqlcommt.executenonquery ();
}
catch (SqlException Sqle)
{
return 0;
}
Finally
{
Sqlconntemp.close ();
}
}
public static SqlConnection Getsqlconnection ()
{
Try
{
Try
{
Sqlconns[count]. Close ();
}
catch (Exception EeX)
{
}
return Sqlconns[count];
}
Finally
{
Current number from Add ...
if (count = = (maxCount-1))
{
Count = 0;
}
Else
{
count++;
}
}
}