C# 将检索到的部门SQL值分配给会话[“UserAuthentication”]

C# 将检索到的部门SQL值分配给会话[“UserAuthentication”],c#,asp.net,mysql,C#,Asp.net,Mysql,我正在从表ts_dept中使用以下SQL查询wirrten从C中检索dept的值-当CurrentName!=空的 只是从内存开始,而不是在我的C机器上,但请尝试以下方法: object CurrentName = com.ExecuteScalar(); if (CurrentName != null && CurrentName != System.DBNull.Value) { { Session["UserAuthentication"] = (s

我正在从表ts_dept中使用以下SQL查询wirrten从C中检索dept的值-当CurrentName!=空的


只是从内存开始,而不是在我的C机器上,但请尝试以下方法:

object CurrentName = com.ExecuteScalar();
if (CurrentName != null && CurrentName != System.DBNull.Value) {
    {
        Session["UserAuthentication"] = (string)CurrentName;
        Session.Timeout = 1;
        Response.Redirect("Default.aspx");
    }
    else
    {
        Session["UserAuthentication"] = "";
    }
}
如果调用正确,则如果查询未返回任何结果,CurrentName将为null;如果查询返回结果但ts_dept.dept为null,则System.DBNull.Value将为null


还要注意关于使用会话进行身份验证的评论——如果您在负载平衡的集群中,那么它显然不起作用。开始将其切换到表单身份验证。

始终使用。这类代码可以被攻击。你的问题不是很清楚。您想将什么分配给什么?我已更新代码以包含参数化查询:-。我要做的是分配dept的值。当CurrentName!=null,即找到匹配项。是否要执行会话[UserAuthentication]=CurrentName?切勿将会话用于身份验证目的。只要工作进程回收,会话就会被清除,并且不利于负载平衡。它也不安全,因为会话cookie很容易被劫持。使用内置的FormsAuthenticationCookie系统。
object CurrentName = com.ExecuteScalar();
if (CurrentName != null && CurrentName != System.DBNull.Value) {
    {
        Session["UserAuthentication"] = (string)CurrentName;
        Session.Timeout = 1;
        Response.Redirect("Default.aspx");
    }
    else
    {
        Session["UserAuthentication"] = "";
    }
}