//數據庫執行區,
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++;
}
}
}