这是我的部分代码片段
//登录按钮触发的事件函数
protected void button1_Click(object sender, EventArgs e)
{
if(LoginData.fucLogin(UserName.Text, PassWord.Text))
{
Response.Redirect("WebForm1.aspx");
}
else
{
label3.Text = "用户名或密码错误,请重新输入";
}
}
//这是校验登录密码函数写到类里面去了
public static bool fucLogin(string username, string password)
{
bool success = false;
SqlConnection myConnection = new SqlConnection(conStr);
myConnection.Open();
SqlCommand objCmd = new SqlCommand("select * from userinfo where UserName='" + username + "'",myConnection);
try
{
SqlDataReader myReader = objCmd.ExecuteReader();
if (myReader.Read())
{
if (myReader["PassWord"].ToString() == password)
{
success = true;
}
}
}
catch (SqlException ex)
{
success = false;
}
finally
{
if (myConnection.State == ConnectionState.Open)
{
myConnection.Close();
}
}
return success;
}
这是数据库里面的数据
我能够成功注册用户然后把数据写入数据库,但是用新注册的用户名和密码就是登录不进去。
如果把if判断语句注释掉变成这样
try
{
SqlDataReader myReader = objCmd.ExecuteReader();
if (myReader.Read())
{
//if (myReader["PassWord"].ToString() == password)
//{
success = true;
//}
}
}
就能输入用户名pan 密码随便输入都能登录进去,我觉得那个if语句有问题但是又看不出哪里出错了。。。小菜鸡刚开始学asp.net之前没接触过c#和数据库。。。还请各位指点指点