[英]How can I retrieve specific data from sql in c#?
我有一个包含下表的数据库:
我正在尝试获取当前用户名的角色,以便我可以将该人重定向到正确的页面。 我用if子句(acc.u_role == 1)尝试了它,但不幸的是它没有用。
public ActionResult Verify(User acc)
{
connectionString();
con.Open();
com.Connection = con;
com.CommandText = "select * from Users where u_username = '"+acc.u_username+"' and u_password ='"+acc.u_password+"'";
dr = com.ExecuteReader();
if (dr.Read())
{
if (acc.u_role == 1) {
con.Close();
return View("AdminHome");
}
else
{
con.Close();
return View("UserHome");
}
}
else
{
con.Close();
return View("Error");
}
}
你在哪里设置acc.u_role?
看起来你正在读取数据然后不使用它。 dr [5]应该包含数据库中的角色
dr = com.ExecuteReader();
if (dr.Read())
{
if (dr[5] == 1) {
con.Close();
return View("AdminHome");
}
我已经找到了我的答案的解决方案,这里是:
if (dr.Read())
{
if (Convert.ToInt32(dr["u_role"]) == 1) {
con.Close();
return View("AdminHome");
}
else
{
con.Close();
return View("UserHome");
}
}
else
{
con.Close();
return View("Error");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.