簡體   English   中英

如何從c#中的sql中檢索特定數據?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM