using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Configuration; using System.Data.SqlClient; using System.Text; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void login_Click(object sender, EventArgs e) { string staid, password, sql_str; staid = Request.Form.Get("sta_id"); password = Request.Form.Get("password"); sql_str = "select top 1 * from tb_staff where sta_id = '" + staid + "' and password ='" + password + "'"; SqlConnection tcon = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]); tcon.Open(); SqlCommand tcmd = new SqlCommand(sql_str, tcon); SqlDataReader tdr = tcmd.ExecuteReader(); if (tdr.Read()) { Page.Response.Redirect("report.aspx"); } else { Response.Write("密码或者帐号错误"); } tdr.Close(); tcon.Close(); } }
界面输入编号密码 点登陆后直接跳到 密码或者账号错误,请高手解答一下万分感谢!新手
protected void login_Click(object sender, EventArgs e) { SqlConnection tcon = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]); tcon.Open(); string sql = "select top 1 * from tb_staff where [sta_id]='" + staid.Text + "' and password='" + spassword.Text + "'"; DataTable dt = new DataTable(); SqlDataAdapter da = new SqlDataAdapter(sql, tcon); da.Fill(dt); if (dt.Rows.Count > 0) { Session["sta_id"] = staid.Text; Response.Redirect("report.aspx"); } else { Response.Write("登陆账号或密码错误!!"); } }
自己已经解决 用 DataTable 获取数据
请求大师帮助,谢谢!
看你的代码使用的是拼接字符串,这样的话会被注入的,最好使用参数化。你可以看一下你拼接的字符串拿到数据库里是否会读出数据
最好的办法就是在if那个地方,调试一下。
谢谢你们自己已经解决