using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string getName;
string getPassword;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
String str = "Data Source=yanyan;Initial Catalog=Data;Integrated Security=True;Pooling=False";
SqlConnection coon = new SqlConnection(str);
//打开连接
coon.Open();
//读取数据
SqlCommand cmd = new SqlCommand("select * from UserInfo where Uname='" + UserNameLogin.Text + "'", coon); //创建查询用户名是否存在数据对象
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
getPassword = sdr["Upassword"].ToString();
if (getPassword == PasswordLogin.Text)//密码正确
{
Session["userID"] = UserNameLogin.Text.Trim();//存储用户名
Response.Redirect("SuccessLogin.aspx");
}
else
{
ErrorLable.Text = "密码错误";
ErrorLable.Visible = true;
}
//else
// LabErrorname.Visible = true;
}
coon.Close();
}
我给程序设置了断点,但他不走
if (getPassword == PasswordLogin.Text)//密码正确
{
Session["userID"] = UserNameLogin.Text.Trim();//存储用户名
Response.Redirect("SuccessLogin.aspx");
}
if里的这段代码,但是getpassword(从数据库里取出的值)的值是等于PasswordLogin.Text的
不知道是哪里错了 ,请帮忙看看,谢谢了
asp.net 数据库读取值与用户输入值比较问题
答案:3 悬赏:30
解决时间 2021-02-16 16:07
- 提问者网友:我是我
- 2021-02-15 23:49
最佳答案
- 二级知识专家网友:茫然不知崩溃
- 2021-02-16 01:06
改成
if (sdr.HasRows)
{
sdr.Read();//Read()方法只是去读去读下一条数据而已,HasRows才是表示这个数据集是否存在数据。
getPassword = sdr["Upassword"].ToString();
...
...//只有上面两句改了,下面基本还是按照你原来的做
}
sdr.Dispose();
sdr.Close();//reader一定要记得释放,不然你的程序跑不了多久。
if (sdr.HasRows)
{
sdr.Read();//Read()方法只是去读去读下一条数据而已,HasRows才是表示这个数据集是否存在数据。
getPassword = sdr["Upassword"].ToString();
...
...//只有上面两句改了,下面基本还是按照你原来的做
}
sdr.Dispose();
sdr.Close();//reader一定要记得释放,不然你的程序跑不了多久。
全部回答
- 1楼网友:废途浑身病态
- 2021-02-16 01:56
if (sdr.Read())
{
这里,如果你的数据库里没读出数据来的话。 if (getPassword == PasswordLogin.Text),是执行不到这段代码来的。所以,应该判断一下
if (sdr.Read())
{
}
else
{
//没有此用户。
}
其他的没发现什么。
- 2楼网友:陪伴是最长情的告白
- 2021-02-16 01:16
<p>既然你在alert里边输出,反正都当字符串处理了,没有必要转换成(int):</p> <p>建议:response.write("<script>alert("+<strong>od["top"].tostring()</strong>+")</script>");</p>
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯