为什么我的下列的代码不能进入数据库读取呢?
高手帮帮忙...谢谢!   
protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("server=localhost;database=oooooool;uid=sa;pwd=sa;");
        con.Open();
        SqlCommand cmd = new SqlCommand("select count(*) from login where txtName='"+txtName +"'and txtpwd='"+txtPwd +"'", con);
        int count = Convert.ToInt32(cmd.ExecuteScalar());
        if (count>0)
        {
            Response.Redirect("amin.aspx");
        }
        else
        {
            Response.Redirect("error.aspx");
        }
        con.Close();
     
    }

解决方案 »

  1.   

    int count = Convert.ToInt32(cmd.ExecuteScalar());
    改成int count = cmd.ExecuteNonquery()
    然后看看
      

  2.   

    改为如下:
    protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("server=localhost;database=oooooool;uid=sa;pwd=sa;");
            con.Open();
            SqlCommand cmd = new SqlCommand("select count(*) from login where txtName='"+txtName +"' and txtpwd='"+txtPwd +"'", con);
            DataSet ds =new DataSet();
            int count = cmd.Fill(ds));
            if (count>0)
            {
                Response.Redirect("amin.aspx");
            }
            else
            {
                Response.Redirect("error.aspx");
            }
            con.Close();
         
        }
      

  3.   

    int count = (int)cmd.ExecuteScalar();
    或者
     int count = cmd.ExecuteScalar() as int;
    试试看
      

  4.   

    public DataTable SelectTextCommand(string sql)
    {
    try
    {
    cmd.Parameters.Clear();
    cmd.CommandType=CommandType.Text;
    cmd.CommandText=sql;
    if(cmd.Connection.State!=ConnectionState.Open)
    cmd.Connection.Open();
    DataTable dt=new DataTable();
    SqlDataAdapter da=new SqlDataAdapter(cmd);
    da.Fill(dt);
    return dt;
    }
    catch(Exception ex)
    {
    ex.ToString();
    }
    finally
    {
    if(cmd!=null)
    if(cmd.Connection.State!=ConnectionState.Closed)
    cmd.Connection.Close();
    }
    return null;
    }DataTable dt=SelectTextCommand("select count(*) from login where txtName='"+txtName +"'and txtpwd='"+txtPwd +"'");
    if(dt!=null)
        count=Convert.ToInt32(dt.Rows[0][0]);
      

  5.   

    说说哪个语句错误??
    返回的那个count值是多少???
    你把那个sql语句直接去数据库里执行一下,看有没有结果
      

  6.   

    慢点单步调试吧,用try catch捕获异常,把错误列出来解决
      

  7.   

    表没应该没有txtName这样的字段吧.凭经验觉得你的SQL有问题.
      

  8.   

    可以不用整型数,用string .大多数类型容易转变为string .
     string str =(string)cmd.ExecuteScalar();  
        if (str != null) 
        {
             Response.Redirect("amin.aspx");
        }
      

  9.   

    ("select count(*) from login where txtName='"+txtName +"'and txtpwd='"+txtPwd +"'", con没有空格吧???在and的前面,用分析器先执行下