我是 c# ASP.NET 的新手,我想用登录部分的值插入检查数据库值,我做的一切都是正确的,但是为什么当我输入相同的值时,我仍然继续发现该值不正确在我的数据库中...有什么想法吗?并且我的 dt 行数一直为 0...当我添加参数时有什么问题吗?
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Ng\Documents\Visual Studio 2015\Projects\Assignment6\Assignment6\App_Data\photoCompetition.mdf;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework");
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM [User] WHERE email='@useremail' and password='@password'", con);
cmd.Parameters.Add("@useremail", SqlDbType.Text).Value = emailtext.Text;
cmd.Parameters.Add("@password", SqlDbType.Text).Value = passwordtext.Text;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
int i = cmd.ExecuteNonQuery();
con.Close();
if (dt.Rows.Count > 0)
{
Response.Redirect("Membermenu.aspx");
}
else
{
lblMsg.Text = "Your username and password is incorrect";
lblMsg.ForeColor = System.Drawing.Color.Red;
emailtext.Text = "";
passwordtext.Text = "";
}
}
使用参数时不需要将单引号设置为字符串,删除查询中的引号
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Ng\Documents\Visual Studio 2015\Projects\Assignment6\Assignment6\App_Data\photoCompetition.mdf;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework");
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM [User] WHERE email=@useremail and password=@password", con);
cmd.Parameters.Add("@useremail", SqlDbType.Varchar).Value = emailtext.Text;
cmd.Parameters.Add("@password", SqlDbType.Varchar).Value = passwordtext.Text;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
int i = cmd.ExecuteNonQuery();
con.Close();
if (dt.Rows.Count > 0)
{
Response.Redirect("Membermenu.aspx");
}
else
{
lblMsg.Text = "Your username and password is incorrect";
lblMsg.ForeColor = System.Drawing.Color.Red;
emailtext.Text = "";
passwordtext.Text = "";
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句