今天遇到个问题代码如下:
private void Button1_Click(object sender, EventArgs e)
{

string userid=zhanghao.Text.ToString();
string pwd=mima.Text.ToString();
string connectionstring="server=ZDC;database=shop;User ID=sa;pwd= ";
SqlConnection   cnn=new SqlConnection(connectionstring);
SqlCommand cmd=cnn.CreateCommand();
cmd.CommandText="SELECT count(*) as icount from customer where userid='"+userid+"'";
cnn.Open();
SqlDataReader reader=cmd.ExecuteReader();
            reader.Read();
            string count=reader["icount"].ToString();
            reader.Close();
if(count=="0")
{Label2.Text="帐号不存在";}
else 
{

 
SqlCommand cmd1=cnn.CreateCommand();
cmd1.CommandText="select * from customer where userid='"+userid+"' "; SqlDataReader reader1=cmd1.ExecuteReader();
reader1.Read();
string mima1=reader1["password"].ToString();
                 reader1.Close();
if(mima1!=pwd)
{Label2.Text="密码不正确";}

else
{
Response.Cookies["customerid"].Value="+useid+";
Response.Redirect("webform1.aspx");}
                    
}
cnn.Close();
}
}
}
最后密码一样时怎么比较都是不正确,但是mima1 和pwd经测试都一样啊。用"zdc321zdc"(密码)就能查出是一样的。

解决方案 »

  1.   

    没写清楚,补充:
    用"zdc321zdc" (密码)替换pwd就能查出是一样的
      

  2.   

    试试string pwd=mima.Text.Trim();
      

  3.   

    string mima1=reader1["password"].ToString();
    这,句估计有问题,没有将两边的空格清除,还有就是输入的信息也没有将空格清除。
      

  4.   

    都带上 trim 都打印出来 看看
      

  5.   

    密码列的数据类型是用char还是varchar,用char型的没达到指定位数会补空格的,所以要消除空格。
    还有你的程序可以稍微改进一下。SqlCommand cmd1=cnn.CreateCommand();
    cmd1.CommandText="select * from customer where userid='"+userid+"' ";
    SqlDataReader reader1=cmd1.ExecuteReader();
    if(reader1.HasRows)
    {
    reader1.Read();
    string mima1=reader1["password"].ToString();
    判断密码
    }
    else
    {
    没有这个用户
    }
      

  6.   

    string mima1=reader1["password"].ToString().Trim();