asp.net access 登陆界面,用户名跟密码验证,但是没有效果,可以直接转到别的页面

Macy0606 2012-03-28 12:26:44
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 = "密码没有填写!";

}
}


我可以运行(但是没有判断用户名跟密码是否正确,请高手帮我修改下,只要能实现验证即可,感谢,比较急
}
...全文
306 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
爱宝宝爱老公 2013-09-22
  • 打赏
  • 举报
回复
你怎么改的啊。我做的不起作用啊
fa120508 2012-05-06
  • 打赏
  • 举报
回复
急求改好了程序
無葉 2012-03-28
  • 打赏
  • 举报
回复
if (state == 1 )//如果数据中没有记录或有多条记录则抱错
{
this.Label2.Text = "登入成功!" ;


}
else
{
this.Label2.Text = "用户不存在,请检测用户名和密码是否正确!";

}

为什么 不把逻辑判断写在前面吗。如:
if (this.username.Text == "")//判断用户名是否未空
{
this.Label2.Text = "密码没有填写!";

return ;
}

EnForGrass 2012-03-28
  • 打赏
  • 举报
回复
if (state == 0 || state > 1)//如果数据中没有记录或有多条记录则抱错
{
this.Label2.Text = "用户不存在,请检测用户名和密码是否正确!";
}
这个要来干嘛?
Macy0606 2012-03-28
  • 打赏
  • 举报
回复
界面是 username password
数据库userinfo 字段 username password
Macy0606 2012-03-28
  • 打赏
  • 举报
回复
改好了,写些各位!!
EnForGrass 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

if (Page.IsValid)
{

if (this.username.Text != "")//判断用户名是否未空
{
if (this.password.Text != "")//判断密码是否未空
{

……
[/Quote]
你先调试state的值是什么?如果state > 0表示有这条记录存在,应该是登陆成功,否则返回0就是没这条记录,
baobei1314520a 2012-03-28
  • 打赏
  • 举报
回复
方法用错了吧?用ExecuteNonQuery()吧应该?
Macy0606 2012-03-28
  • 打赏
  • 举报
回复
if (Page.IsValid)
{

if (this.username.Text != "")//判断用户名是否未空
{
if (this.password.Text != "")//判断密码是否未空
{

string sql;
sql = "select * from userinfo where username='" + this.username.Text + "' and password='" + this.password.Text + "'";//建立sql查询语句

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 = "登入成功!";
Response.Redirect("Detail.aspx");
}
conn.Close();
}
}
}

现在改成这个也不行!!!!!!
Macy0606 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

C# code


if (this.username.Text != "")//判断用户名是否未空
{
if (this.password.Text != "")//判断密码是否未空
{

string sql;
sql = "select count(*) from userinfo where username='" + this.username.Text + "……
[/Quote]
我使用你发的这个后还是没有判断就直接跳转到下个页面了
Macy0606 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

另外···不要把文本框内容直接拼凑语句查询···会把不安全字符加入查询语句的···
[/Quote]
能否把修改后的发给我试试
lbx541575387 2012-03-28
  • 打赏
  • 举报
回复
另外···不要把文本框内容直接拼凑语句查询···会把不安全字符加入查询语句的···
lbx541575387 2012-03-28
  • 打赏
  • 举报
回复
try里面有语句错误,把try去掉调试
EnForGrass 2012-03-28
  • 打赏
  • 举报
回复


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 = "密码没有填写!";

}
Im_Sorry 2012-03-28
  • 打赏
  • 举报
回复
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查询语句
Macy0606 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

if (state == 0 || state > 1)//如果数据中没有记录或有多条记录则抱错
{
this.Label2.Text = "用户不存在,请检测用户名和密码是否正确!";
}
这个要来干嘛?
[/Quote]就是判断是否有数据啊

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧