出现"SqlTransaction 已完成;它再也无法使用"的错误提示之解决方案下簈

Admin | 2007-12-31 22:15:33 | TrackRecord: 2766 Times | Tag标签:asp.net 打印本页

您当前所处的位置是:〖首页〗→【文章页】 本站共有16个图文教程栏目,请用心拜读!

本站提供经典的Excel公式函数实例,Word排版技巧,PPT教程;同时更兼有Flash,PowerPoint,数据库等技术文章。

 批处理事务不应该把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的启用时机。

网络转载



上一篇:出现"SqlTransaction 已完成;它再也    下一篇:HTTP(401,500)错误代码详细介绍

会员评论列表:
针对本篇文章或本站,请您发表个人的建议或批评!
FreeBoxPc

谷歌搜索 百度搜索 本站仅与内容具备一定的实用价值的原创网站交换友情链接,力争为大众做出更优质的服务!
All Rights Reserved版权所有 本站备案信息:滇ICP备11001339号-2 站长联系方式 Email:dzwebs@126.com