程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net 執行事務代碼

asp.net 執行事務代碼

編輯:ASP.NET基礎
//數據庫執行區,
SqlConnection _sqlConn = null;
SqlTransaction _SqlTrans = null;
try
{
//數據庫操作類
_sqlConn = System.mySQLConnection.SqlConnections.GetSQLConnection();//見下面的類
_sqlConn.Open();
// sql事務對象
_SqlTrans = _sqlConn.BeginTransaction();
//基本信息執行SQL語句情況
bool StudentInfo_YN = false;
if (hid_StudentIDValue == "")
{
StudentInfo_YN = (System.mySQLConnection.SqlConnections.UpdateOrDelete(_sqlConn, _SqlTrans, sql語句) > 0) ? true : false;
}
else
{
StudentInfo_YN = true;
}
bool baoMing_YN = false;
//報名基本信息執行SQL語句情況
if (StudentInfo_YN)
{
//獲取報名的o_flowid號,由數據庫產生
baoMing_YN = (System.mySQLConnection.SqlConnections.UpdateOrDelete(_sqlConn, _SqlTrans, sql語句) > 0) ? true : false;
}
else
{
baoMing_YN = false;
}
bool baomingdetail_YN = false;
//報名的課程詳細信息執行SQL語句情況
if (baoMing_YN)
{
baomingdetail_YN = (System.mySQLConnection.SqlConnections.UpdateOrDelete(_sqlConn, _SqlTrans, sql語句) > 0) ? true : false;
}
else
{
baomingdetail_YN = false;
}
//收費表SQL語句情況
//if (baoMing_YN)
//{
// baomingdetail_YN = (System.mySQLConnection.SqlConnections.UpdateOrDelete(_sqlConn, _SqlTrans, sql語句) > 0) ? true : false;
//}
//else
//{
// baomingdetail_YN = false;
//}
//總的判斷全部釋放成功。
if (baomingdetail_YN)
{
//提交事務,清除Session驗證碼
_SqlTrans.Commit();
MessageBox.alert(Page, "報名成功。", "window.location.href='addBaoMing.aspx'");
}
else
{
_SqlTrans.Rollback();
MessageBox.alert(Page, "報名失敗。");
return;
}
}
catch (SqlException sqlE)
{
MessageBox.alert(Page, "提交異常 " + sqlE.Message.Replace("\"", "'"));
}
finally
{
if (_sqlConn != null)
_sqlConn.Close();
if (_SqlTrans != null)
_SqlTrans.Dispose();
}
寫在SqlConnections的類中(一般放在app_code,也可放在三層架構的類中)
//當前連接的是第幾個。
static int count = 0;
//最大連接量
static int maxCount = 30;
static SqlConnection[] sqlConns = new SqlConnection[maxCount];
//自己創建自己
static SqlConnections myConn = new SqlConnections();
//初始化
private SqlConnections()
{
for (int i = 0; i < maxCount; i++)
{
sqlConns[i] = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ADOStr"].ToString());
}
}
// 獲取數據庫連接
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
{
//當前個數自加。。
if (count == (maxCount - 1))
{
count = 0;
}
else
{
count++;
}
}
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved