错误提示:ExecuteScalar 要求已打开且可用的连接。连接的当前状态为已关闭。
源代码如下:请问是什么原因啊?
protected void Page_Load(object sender, EventArgs e)
    {
        string userName = Request.Form["userName"].ToString();
        string userPwd = Request.Form["userPwd"].ToString();        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=login;Integrated Security=True");
        string s = "select count(*) from login where userName='"+userName+"'and userPwd='"+userPwd+"'";
        SqlCommand cmd1 = new SqlCommand(s,con);
        
        try
        {            int count = Convert.ToInt32(cmd1.ExecuteScalar());
            if (count > 0)
            {
                Response.Redirect("main.aspx");
            }
            else
            {
                Response.Redirect("loginfail.htm");
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        } 

解决方案 »

  1.   

    是不是  SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=login;Integrated Security=True");  出错了。检查下sql server打开没。
      

  2.   

    试试: protected void Page_Load(object sender, EventArgs e)
        {
            string userName = Request.Form["userName"].ToString();
            string userPwd = Request.Form["userPwd"].ToString();        int i = getTotals(userName, userPwd);       
            
            if (i > 0)
            {
                Response.Redirect("main.aspx");
            }
            else
            { 
                Response.Redirect("loginfail.htm");
            }
        }    private int getTotals(string strName, string strPwd)
        {
            int i = 0;
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=login;Integrated Security=True");
            string s = "select count(*) from login where userName='" + strName + "'and userPwd='" + strPwd + "'";
            SqlCommand cmd1 = new SqlCommand(s, con);
            con.Open();
            i = Convert.ToInt32(cmd1.ExecuteScalar());
            con.Close();
            return i;
        }
      

  3.   


     顶 3 楼 Open下就好了
      

  4.   

    错误提示:ExecuteScalar 要求已打开且可用的连接。连接的当前状态为已关闭。LZ错误提示已经说明连接出了一定的问题,连接的当前状态为已关闭。在使用连接时,它必须是打开的,使用完后再关闭/
    希望lz在以后出现错误或者异常的时候可以学会看懂错误提示,这样对自己的调式和以后开发程序的速度有很大的提高
      

  5.   

    protected void Page_Load(object sender, EventArgs e) 
        { 
            string userName = Request.Form["userName"].ToString(); 
            string userPwd = Request.Form["userPwd"].ToString();         SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=login;Integrated Security=True"); 
            string s = "select count(*) from login where userName='"+userName+"'and userPwd='"+userPwd+"'"; 
            SqlCommand cmd1 = new SqlCommand(s,con); 
             
            try 
            { 
                con.open
                int count = Convert.ToInt32(cmd1.ExecuteScalar()); 
                    con.close
                if (count > 0) 
                { 
             
                    Response.Redirect("main.aspx"); 
                } 
                else 
                { 
                    Response.Redirect("loginfail.htm"); 
                } 
            } 
            catch (Exception ex) 
            { 
                Response.Write(ex.Message); 
            } 
      

  6.   


    protected void Page_Load(object sender, EventArgs e) 
        { 
            string userName = Request.Form["userName"].ToString(); 
            string userPwd = Request.Form["userPwd"].ToString();         SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=login;Integrated Security=True"); 
            string s = "select count(*) from login where userName='"+userName+"'and userPwd='"+userPwd+"'"; 
            SqlCommand cmd1 = new SqlCommand(s,con); 
             
            try 
            { 
                con.open();//打开连接
                int count = Convert.ToInt32(cmd1.ExecuteScalar()); 
                if (count > 0) 
                { 
                    Response.Redirect("main.aspx"); 
                } 
                else 
                { 
                    Response.Redirect("loginfail.htm"); 
                } 
            } 
            catch (Exception ex) 
            { 
                Response.Write(ex.Message); 
            } 
            finally
            {//关掉
                 con.close();
                 con.dispose();
            }
      

  7.   

    基本功问题,楼主的书看得太少了.
    con.open();方法都没有啊,怎么读数据?在读取数据库之前要打开数据库
    con.open();
    SqlCommand cmd1 = new SqlCommand(s,con); 
    -------------最后
     finally
    {
     con.close();