将深度检索到的SQL值分配给会话["UserAuthentication"]
本文关键字:quot 会话 UserAuthentication 分配 检索 深度 SQL | 更新日期: 2023-09-27 18:05:23
我正在从表ts_dept中用c#编写的SQL查询中检索dept的值-当(CurrentName != null)
时,我如何将其分配给Session["UserAuthentication"]
?
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string username = Login_Box.UserName;
string pwd = Login_Box.Password;
string strConn;
strConn = WebConfigurationManager.ConnectionStrings["team13ConnectionString"].ConnectionString;
SqlConnection Conn = new SqlConnection(strConn);
Conn.Open();
string sqlUserName;
sqlUserName = "SELECT dept FROM ts_dept WHERE id=@username AND pass=@pwd";
SqlCommand com = new SqlCommand(sqlUserName, Conn);
com.Parameters.Add("@username", username);
com.Parameters.Add("@pwd", pwd);
string CurrentName;
CurrentName = (string)com.ExecuteScalar();
if (CurrentName != null)
{
Session["UserAuthentication"] = username;
Session.Timeout = 1;
Response.Redirect("Default.aspx");
}
else
{
Session["UserAuthentication"] = "";
}
}
只是从内存这里(不是在我的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。
还要注意关于使用Session进行身份验证的注释—如果您在负载均衡的集群中,它将无法正常工作。