“SqlTransaction 已完成;它再也无法使用”解决方法
来源:互联网 发布:运12飞机发动机数据 编辑:程序博客网 时间:2024/06/05 15:17
“SqlTransaction 已完成;它再也无法使用”解决方法
当只是使用一次事务时,只用简单的事务就可以了
示例代码:
SqlServerDataBase obj = new SqlServerDataBase();
SqlConnection conn = obj.DBconn();
conn.Open();
SqlTransaction myTrans;
myTrans = conn.BeginTransaction(IsolationLevel.ReadCommitted);
try
{
obj.Insert("insert into StuInfo (StuNumber,Name,Sex,Specialty) values('" + stuNumber + "','" + name + "','" + sex + "','" + specialty + "')", null);
obj.Insert("insert into StuSkill(StuNumber,Skill) values('" + stuNumber + "','" + skill + "')", null);
myTrans.Commit();
Response.Write("两条数据均插入到数据库中!");
}
catch (Exception ex)
{
try
{
myTrans.Rollback();
}
catch (SqlException sqlEx)
{
if (obj.DBconn() != null)
{
Response.Write(sqlEx.GetType()+"数据库打开失败");
Response.Redirect("submit.htm");
}
}
}
finally
{
obj.DBconn().Close();
}
但是设计多个事务处理,比方使用for循环时,处理不当就会产生“SqlTransaction 已完成;它再也无法使用”的问题,主要就是因为SqlTransaction 使用了Commit()后就是不能在使用了,必须重新BeginTransaction()一个。
批处理事务不应该把transaction事务放在for循环里,造成多个事务,这将导致错误.
应该把for放在transaction里,当成一个事务来处理,进行批处理
示例代码:
protected void btnDelete_Click(object sender, EventArgs e) //删除
{
SqlServerDataBase del = new SqlServerDataBase();
string str = "";
SqlConnection conn = del.DBconn();
conn.Open();
SqlTransaction myTrans;
myTrans = conn.BeginTransaction(IsolationLevel.ReadCommitted);
ListBox1.Items.Clear();
try
{
for (int i = 0; i < GridView1.Rows.Count; i++) //从GridView中选中checkbox
{
GridViewRow row = GridView1.Rows[i];
bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;
if (isChecked)
{
ListBox1.Items.Add(GridView1.Rows[i].Cells[2].Text);
//Column 1 is the StuNumber column
str = GridView1.Rows[i].Cells[1].Text; //获取删除行的StuNumber的值
// Response.Write(str.ToString());
del.Delete("delete from StuInfo where StuNumber = '" + str + "'", null);
del.Delete("delete from StuSkill where StuNumber = '" + str + "'", null);//删除
}
}
myTrans.Commit();
}
catch (Exception ex)
{
try
{
myTrans.Rollback();
}
catch (SqlException sqlEx)
{
if (del.DBconn() != null)
{
Response.Write(sqlEx.GetType() + "数据库打开失败");
Response.Redirect("submit.htm");
}
}
}
finally
{
conn.Close();
}
BindData();
}
}
注意:两者在使用上是有区别的!
另外在windows服务中,
windows的服务服务正常运行一段时间后,在ontimer中就无法再启动了,提示:此SqlTransaction已完成;它再也无法使用。
有可能是 timer 时间间隔 太短,造成事务太频繁!
也可能是Transaction两次提交的原因,需要注意一下timer的启用时机。
参考网站:< src="http://wz.csdn.net/javascripts/vote.js" type="text/javascript">
http://blog.csdn.net/kiki113/archive/2007/04/29/1591999.aspx
http://topic.csdn.net/u/20070830/10/62719c5d-cb7a-47e5-87a3-89077d82dc77.html
- “SqlTransaction 已完成;它再也无法使用”解决方法
- “SqlTransaction 已完成;它再也无法使用”解决方法
- “SqlTransaction 已完成;它再也无法使用”解决方法
- 会话状态已创建一个会话 ID,但由于响应已被应用程序刷新而无法保存它 解决方法之一
- 文件隐藏后再也无法看到的解决方法.
- 未能打开.jpg它可能已损坏,或者可能使用了“预览”无法识别的文件格式
- 做文件移动时,提示“操作无法完成,应为文件已在SQL Server(MSSQLSERVER)中打开解决方法
- 【mac】terminal终端 出现[进程已完成]无法使用终端的情况
- SqlTransaction
- SqlTransaction
- 还原数据库时出错“无法处理数据库 ,因为它正由此会话使用”的解决方法
- ASP.NET 如何使用 SqlTransaction
- adb已配置完成,却无法调试手机
- yum 无法使用解决方法
- google无法使用解决方法
- IE错误“Java(TM) 已被阻止,因为它已过时并且需要更新。”的解决方法
- IE错误“Java(TM) 已被阻止,因为它已过时并且需要更新。”的解决方法
- SqlTransaction 数据库编程事务使用示例
- 关于GridView翻页checkBox状态保存的问题
- SoInput, SoOutput, SoWriteAction, SoSearchAction使用
- 需要自己学会习惯的
- 跟我一起写 Makefile
- Eucalyptus上传镜像提示没有权限
- “SqlTransaction 已完成;它再也无法使用”解决方法
- 查看mysql版本
- ASP.NET的六大内置对象总结
- 赚积分
- LCDS COUT
- [Photo] Share photos of my cute family
- welcome-file-list 引发的血案
- chap2:信息的表示和处理
- 给我留个门