使用 c# 如何接收选择语句 (SQL) 的值

本文关键字:SQL 的值 语句 选择 何接收 使用 | 更新日期: 2023-09-27 18:34:16

我想将选择语句的结果值检索到字符串变量中。实际上我做了成功的登录编码,但之后我想根据他们的角色重定向页面从检索到数据库,如果这是管理员,那么它会转到管理员主页,如果这是用户,那么它会转到用户主页。在我的代码下面,请检查我错在哪里。

protected void btn_lpc_Login_Click(object sender, EventArgs e)
{
        string strCon = ConfigurationManager.ConnectionStrings["sessionname"].ConnectionString;
        string strSelect = "SELECT COUNT(*) FROM LOGIN_DETAILS WHERE EMAIL_ID=@EMAIL_ID AND PASSWORD =@PASSWORD";
    SqlConnection con = new SqlConnection(strCon);
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = strSelect;
    SqlParameter username = new SqlParameter("@EMAIL_ID", SqlDbType.NVarChar, 36);
    username.Value = tbx_lpc_username.Text.Trim().ToString();
    cmd.Parameters.Add(username);
    SqlParameter password = new SqlParameter("@PASSWORD", SqlDbType.NVarChar, 50);
    password.Value = tbx_lpc_password.Text.Trim().ToString();
    cmd.Parameters.Add(password);
    con.Open();
    int result = (Int32)cmd.ExecuteScalar();

在这里说我的怀疑:

    if (result >= 1)
    {
        string strSelect2 = "SELECT ROLEID FROM LOGIN_DETAILS WHERE EMAIL_ID=@EMAIL_ID";
        if (srole == "USER")
        {
            Session.Add("usnm", tbx_lpc_username.Text);
            Response.Redirect("~/ADMIN_PAGES/LPC_ADM_HomePage.aspx");
        }
        else if (srole == "ADMIN")
        {
            Session.Add("usnm", tbx_lpc_username.Text);
            Response.Redirect("~/ADMIN_PAGES/LPC_ADM_SettingsPage.aspx");
        }
    }
    else
    {
        ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
    }
   }

使用 c# 如何接收选择语句 (SQL) 的值

    string strSelect2 = "SELECT ROLEID FROM LOGIN_DETAILS WHERE EMAIL_ID=@EMAIL_ID";
    cmd.CommandText = strSelect2;
    username = new SqlParameter("@EMAIL_ID", SqlDbType.NVarChar, 36);
    username.Value = tbx_lpc_username.Text.Trim().ToString();
    cmd.Parameters.Add(username);

    int roleId = (Int32)cmd.ExecuteScalar();
    if (roleId == 2) //USER
        {
            Session.Add("usnm", tbx_lpc_username.Text);
            Response.Redirect("~/ADMIN_PAGES/LPC_ADM_HomePage.aspx");
        }
        else if (roleId == 1) // ADMIN
        {
            Session.Add("usnm", tbx_lpc_username.Text);
            Response.Redirect("~/ADMIN_PAGES/LPC_ADM_SettingsPage.aspx");
        }