将会话数据存储到表中

本文关键字:存储 会话 数据 | 更新日期: 2023-09-27 18:34:28

我可以通过单击按钮将会话值存储在表中吗?在下面的代码中,我希望表中UserName

这是我正在使用的代码:

string cmdstr = "select count(*) from Reg where UserName='" + TextBoxaun.Text + "'";
SqlCommand checkUser = new SqlCommand(cmdstr, con);
int temp = Convert.ToInt32(checkUser.ExecuteScalar().ToString());
if (temp == 1)
{
    string cmdstr2 = "select Password from Reg where UserName='" +   TextBoxaun.Text + "'";
    SqlCommand pass = new SqlCommand(cmdstr2, con);
    string password = pass.ExecuteScalar().ToString();
    con.Close();
    if  (password == TextBoxapass.Text)
        Session["newapply"] = TextBoxaun.Text;
}

将会话数据存储到表中

首先,您需要 ASP.NET SQL Server注册工具

创建 ASPState 数据库后,必须在 web.config 中更改会话状态模式,如下所示:

<sessionState mode="SQLServer" 
              sqlConnectionString="[Database connection string]"/> 

附言使用 SQLServer 或 StateServer 模式时,对象必须是可序列化的。有关更多详细信息,请参阅会话状态模式