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>");
}
}
虽然提问和代码槽点很多,但是一眼看过去,SQL语句都写错了, select...from
嗯嗯 直接换了写法 但是我下面这个写法好像如果页面输入直接数据库没有的账号和密码好像不好提示
SQL字符串写错了,还有就是,这种写法拼接SQL,是最不合理的写法,
建议下载一个李天平老师的代码生动器, 这种webform写法, 学习一下这个
感谢 前辈 这是插件???? 我用的是VS2019 直接下载这个插件?? 我不太懂你说的什么意思
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 >");
}