asp.net Execute transaction code _ Practical Tips

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++;
}
}
}
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.