asp.net access 登陆界面,用户名跟密码验证,但是没有效果,可以直接转到别的页面
if (Page.IsValid)
{
if (this.username.Text != "")//判断用户名是否未空
{
if (this.password.Text != "")//判断密码是否未空
{
string sql;
sql = "select count(*) from userinfo where username='" + this.username.Text + "' and pwd='" + this.password.Text + "'";//建立sql查询语句
try
{
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("./App_Data/db1.mdb"));//建立数据库连接
conn.Open();
OleDbCommand cmd = new OleDbCommand(sql, conn);
int state =Convert.ToInt32( cmd.ExecuteScalar());//执行sql语句,并返回获得值
if (state == 0 || state > 1)//如果数据中没有记录或有多条记录则抱错
{
this.Label2.Text = "用户不存在,请检测用户名和密码是否正确!";
}
else
{
this.Label2.Text = "登入成功!" ;
}
conn.Close();
}
catch (Exception a)
{
Response.Redirect("Detail.aspx");
}
}
else
{
this.Label2.Text = "验证码不正确,请重新输入!";
}
}
else
{
this.Label2.Text = "验证码没有填写!";
}
}
else
{
this.Label2.Text = "密码没有填写!";
}
}
我可以运行(但是没有判断用户名跟密码是否正确,请高手帮我修改下,只要能实现验证即可,感谢,比较急
}
------解决方案-------------------- if (state == 0 || state > 1)//如果数据中没有记录或有多条记录则抱错
{
this.Label2.Text = "用户不存在,请检测用户名和密码是否正确!";
}
这个要来干嘛?
------解决方案--------------------if (state == 1 )//如果数据中没有记录或有多条记录则抱错
{
this.Label2.Text = "登入成功!" ;
}
else
{
this.Label2.Text = "用户不存在,请检测用户名和密码是否正确!";
}
为什么 不把逻辑判断写在前面吗。如:
if (this.username.Text == "")//判断用户名是否未空
{
this.Label2.Text = "密码没有填写!";
return ;
}
------解决方案-------------------- sql = "select count(*) from userinfo where username='" + this.username.Text + "' and pwd='" + this.password.Text + "'";//建立sql查询语句
你的查询语句有毛病。你这是查询符合条件的总条数的
应该这样
sql = "select * from userinfo where username='" + this.username.Text + "' and pwd='" + this.password.Text + "'";//建立sql查询语句
------解决方案--------------------
C# code
if (this.username.Text != "")//判断用户名是否未空
{
if (this.password.Text != "")//判断密码是否未空
{
string sql;
sql = "select count(*) from userinfo where username='" + this.username.Text + "' and pwd='" + this.password.Text + "'";//建立sql查询语句
try
{
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("./App_Data/db1.mdb"));//建立数据库连接
conn.Open();
OleDbCommand cmd = new OleDbCommand(sql, conn);
int state =Convert.ToInt32(cmd.ExecuteScalar());//执行sql语句,并返回获得值
if (state > 0)
{
this.Label2.Text = "登入成功!" ;
}
else//如果数据中没有记录或有多条记录则抱错
{
this.Label2.Text = "用户不存在,请检测用户名和密码是否正确!";
}
conn.Close();
}
catch (Exception a)
{
Response.Redirect("Detail.aspx");
}
}
else
{
this.Label2.Text = "验证码不正确,请重新输入!";
}
}
else
{
this.Label2.Text = "验证码没有填写!";
}
}
else
{
this.Label2.Text = "密码没有填写!";
}
------解决方案--------------------
try里面有语句错误,把try去掉调试