注册 登录
编程论坛 ASP.NET技术论坛

ExecuteNonQuery 要求已打开且可用的连接。连接的当前状态为已关闭。

xiaoshi5210 发布于 2010-05-10 17:43, 3999 次点击
protected void lbtnDelete_Command(object sender, CommandEventArgs e)
    {
        if(Session["admin"]!=null)
        {
      
            string userID = ();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = DB.getConnection();
             = "delete from guest where ID='"+userID+"'";
            if (cmd.ExecuteNonQuery() > 0)
            {
                Response.Write("<script>alert('删除成功!');window.location=window.location;</script>");
            }
            else
            {
                Response.Write("<script>alert('删除失败!');window.location=window.location;</script>");
            }
        }


运行时出现“ExecuteNonQuery 要求已打开且可用的连接。连接的当前状态为已关闭。”这样的错误,是怎么回事呢 代码要怎么写啊,请各位帮我看看,谢谢!
3 回复
#2
misswang2010-05-10 20:15
连接没打开,SqlConnection
#3
czyzhuo2010-05-11 09:11
程序代码:

SqlConnection conn = DB.getConnection();

conn.open();

cmd.Connection = conn;


#4
xiaoleizhan2010-05-12 09:47
要打开数据库连接
conn.open();
1