Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用c如何接收select语句(SQL)的值_C#_Sql_Visual Studio 2010_Sql Server 2008_C# 4.0 - Fatal编程技术网

C# 使用c如何接收select语句(SQL)的值

C# 使用c如何接收select语句(SQL)的值,c#,sql,visual-studio-2010,sql-server-2008,c#-4.0,C#,Sql,Visual Studio 2010,Sql Server 2008,C# 4.0,我想将select语句的结果值检索到字符串变量中。事实上,我成功地进行了登录编码,但在此之后,我希望根据他们的角色将页面重定向到数据库,如果是ADMIN,则转到ADMIN主页,如果是USER,则转到USER主页。下面是我的代码,看看我哪里错了 protected void btn_lpc_Login_Click(object sender, EventArgs e) { string strCon = ConfigurationManager.ConnectionStrings[

我想将select语句的结果值检索到字符串变量中。事实上,我成功地进行了登录编码,但在此之后,我希望根据他们的角色将页面重定向到数据库,如果是ADMIN,则转到ADMIN主页,如果是USER,则转到USER主页。下面是我的代码,看看我哪里错了

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>");
    }
   }

您可能应该执行第二个查询,即strSelect2变量中的查询我可以知道问题的解决方案。。。我想这不是我问题的答案。。
    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");
        }