[英]How can I retrieve specific data from sql in c#?
I have a database with the following table: 我有一个包含下表的数据库:
I am trying to get the role of the current username so I can redirect that person to the right page. 我正在尝试获取当前用户名的角色,以便我可以将该人重定向到正确的页面。 I tried it with the if clause (acc.u_role == 1), but it doesn't work unfortunately. 我用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");
}
}
Where are you setting acc.u_role? 你在哪里设置acc.u_role?
It looks like you're reading the data and then not using it. 看起来你正在读取数据然后不使用它。 dr[5] should contain the role from the database dr [5]应该包含数据库中的角色
dr = com.ExecuteReader();
if (dr.Read())
{
if (dr[5] == 1) {
con.Close();
return View("AdminHome");
}
I already found a solution to my answer, here it is: 我已经找到了我的答案的解决方案,这里是:
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.