protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text.Trim() == "" || TextBox2.Text.Trim() == "")
{
Label3.Text = "你还没有输入用户名和密码哩!"; // 检查这二个输入框是否未输入东西便按了提交
}
else
{
SqlConnection conn = new SqlConnection("Data Source=JACKSON;Initial Catalog=xin;User ID=sa;Password=123456");
conn.Open();
SqlDataAdapter adp = new SqlDataAdapter("Select * from tuser where name='" + TextBox1.Text + "'", conn);
DataSet ds = new DataSet();
adp.Fill(ds,"tuser"); conn.Close();
if (ds.Tables[0].Rows.Count == 0)
{
Label3.Text = "对不起,您还没有注册,请先注册,提示"; //如没有记录则表示用户名不存在,需要注册
}
else
{
DataRow[] dr = ds.Tables[0].Select("name='" + TextBox1.Text + "'"); //为取数据表中某行字段内容做准备 数据行数组! if (string.Compare(dr[0]["password"].ToString(), TextBox2.Text.ToString()) == 0) //将密码字符和数据库中Pssword字段的值相比较,是否完全相等,相等为登录成功
//if (dr[0]["password"].ToString().Equals(TextBox2.Text.ToString()))
{
Label3.Text = "登录成功"; //Label1 控件显示登录成功 Response.Write("登录成功,谢谢!");
}
else
{ Label3.Text = "对不起,密码不对!"; }
}
}
}
下一步,我想做一下深度判断,if (string.Compare(dr[0]["password"].ToString(), TextBox2.Text.ToString()) == 0) 这样才能登录验证完成,都正确但是却提示:对不起,密码不对,有劳各位大哥再帮看一下,在此深深感谢啦,谢谢牛人大哥!
这句值不对、
所以调到else去了、
compare比较ds数据行在数据库取得的密码和text2写入的值,都是string的啊,怎么就会出错呢?
是这样、
if (dr[0]["password"].ToString().Equals(TextBox2.Text.ToString())) 这样我也试过啦,也是不对呢!
DataTable dt = ds.Tables[0];
if (string.Compare(dt[0]["password"].ToString(), TextBox2.Text.ToString()) == 0)