asp.net 执行事务代码
程序员文章站
2024-03-09 14:14:59
//数据库执行区, sqlconnection _sqlconn = null; sqltransaction _sqltrans = null; try { //数据库操...
//数据库执行区,
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++;
}
}
}
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++;
}
}
}
上一篇: java生成彩色附logo二维码
下一篇: ASP.NET Cookie 操作实现