我正在尝试创建一个简单的登录页面,用户输入ID和密码,并从下拉列表中选择角色:学生、管理员或教师。代码如下:
protected void loginButton_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=University;Integrated Security=True;Pooling=False";
//myConn.Open();
//string strqry = "Insert into students values (" + TextBox1.Text +
//",'" + TextBox2.Text + "','" + TextBox3.Text + "')";
//SqlCommand myCom = new SqlCommand(strqry, myConn);
//int numrow = myCom.ExecuteNonQuery();
//myConn.Close();
Int32 verify;
string query1 = "Select count(*) from Login where ID='" + idBox.Text + "' and Password='" + passwordBox.Text + "' and Type='"+ LoginAs.Text +"'" ;
SqlCommand cmd1 = new SqlCommand(query1, con);
con.Open();
verify = Convert.ToInt32(cmd1.ExecuteScalar());
con.Close();
if (verify > 0)
{
Response.Redirect("succesful.aspx");
}
else
{
Response.Redirect("unsuccesful.aspx",true);
}
}问题是,当我在没有检查名为"LoginAs“的下拉列表的值的情况下尝试时,它工作得很好,并进行了验证。但是,当我检查类型时,无论是学生、管理员还是教师,即使所有信息都是正确的,它也总是不能成功登录。有人能帮我找出哪里出了问题吗?
谢谢
发布于 2013-05-18 03:45:18
使用参数化查询可能会得到解决,这样可以防止风险
cmd1.Parameters.AddWithValue("@ID", idBox.Text);发布于 2013-05-14 16:05:54
首先要检查的是:
中截断角色
但是,最好使用enum/int来定义user的角色,而不是文本名称。
发布于 2013-05-17 19:37:35
尝试一下SqlDataReader类。如果发生读取,您可以捕获。这是link。虽然情况并非如此,但我建议您在数据库中使用标量函数,或者使用EntityFramework或其他对象关系管理工具。
https://stackoverflow.com/questions/16537930
复制相似问题