首页 新闻 会员 周边

asp.net登录验证数据库

0
悬赏园豆:80 [已解决问题] 解决于 2022-03-29 14:24

protected void dlu_button1(object sender, EventArgs e)
{
string name = TextBox1.Text;//赋值为前端输入的用户名
string pwd = TextBox2.Text;//赋值为前端输入的密码
string code = tbx_yzm.Text;//验证码
HttpCookie htco = Request.Cookies["ImageV"];
string scode = htco.Value.ToString();
try
{
if (name == "" || pwd == "")
{
Response.Write("<script>alret('请输入账号和密码')</script>");
}
else
{
string str = System.Configuration.ConfigurationManager.ConnectionStrings["Xsa_fruitsDB"].ConnectionString;
// 构造sql查询语句
string sql;
sql = "select name, pwd [User] where name='" + name + "' and pwd='" + pwd + "'";
// 构造连接对象
using (SqlConnection conn = new SqlConnection(str))
{
SqlCommand cmd = new SqlCommand(sql, conn);
// 打开数据库连接
conn.Open();
// 执行查询语句,返回结果集第一行第一列
string tr = cmd.ExecuteScalar().ToString();
if (tr != "")
{
this.tbx_yzm.Text = "";
Response.Write("<script>alert('登录成功');location.href='Home.aspx';</script>");
}
else if (code.ToUpper() != scode)
{
Response.Write("<script>alert('验证码输入不正确!')</script>");
}
else
{
Response.Write("<script>alret('账号或密码错误!')</script>");
}
}
}
}
catch (Exception)
{
Response.Write("<script>alret('数据有异常!')</script>");
}

    }
And杰然的主页 And杰然 | 初学一级 | 园豆:125
提问于:2022-03-20 10:28
< >
分享
最佳答案
0

虽然提问和代码槽点很多,但是一眼看过去,SQL语句都写错了, select...from

收获园豆:80
talentzemin | 小虾三级 |园豆:759 | 2022-03-20 21:13

嗯嗯 直接换了写法 但是我下面这个写法好像如果页面输入直接数据库没有的账号和密码好像不好提示

And杰然 | 园豆:125 (初学一级) | 2022-03-21 09:47
其他回答(2)
0

SQL字符串写错了,还有就是,这种写法拼接SQL,是最不合理的写法,
建议下载一个李天平老师的代码生动器, 这种webform写法, 学习一下这个

风浪 | 园豆:2996 (老鸟四级) | 2022-03-21 09:28

感谢 前辈 这是插件???? 我用的是VS2019 直接下载这个插件?? 我不太懂你说的什么意思

支持(0) 反对(0) And杰然 | 园豆:125 (初学一级) | 2022-03-21 09:49
0

string name = TextBox1.Text;//赋值为前端输入的用户名
string pwd = TextBox2.Text;//赋值为前端输入的密码
string code = tbx_yzm.Text;//验证码
HttpCookie htco = Request.Cookies["ImageV"];
string scode = htco.Value.ToString();
try {
if (name != "" && pwd != "" && code != "")
{
string sql = string.Format("select * from AndUser where name='{0}'and pwd='{1}'", name, pwd);
List<AndUser> list = AndUserDAL.select(sql);
if (list.Count > 0)
{
Response.Write("<script>alert('登录成功');location.href='Home.aspx';</script>");
}else
{
Response.Write("<script>alert('您还未注册该用户!')</script >");
TextBox1.Text = "";
TextBox2.Text = "";
tbx_yzm.Text = "";
}

            }else if (name == "" && pwd == "" && code == "")
            {
                Response.Write("<script>alert('账号密码不能为空!')</script >");
                TextBox1.Text = "";
                TextBox2.Text = "";
                tbx_yzm.Text = "";
            }else if (code.ToUpper() != scode)
            {
                Response.Write("<script>alert('验证码输入不正确!')</script>");
            }else
            {
                Response.Write("<script>alert('数据异常')</script >");
            }
        } catch (Exception) {

            Response.Write("<script>alert('数据异常')</script >");
        }
And杰然 | 园豆:125 (初学一级) | 2022-03-21 09:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册