将会话数据存储到表中

时间:2012-05-16 18:48:39

标签: c# asp.net sql database-connection session-state

我可以通过点击按钮将会话值存储在表中。在下面的代码中,我想要表格中的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;
}

1 个答案:

答案 0 :(得分:2)

首先,您需要ASP.NET SQL Server Registration Tool

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

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

P.S。使用SQLServer或StateServer模式时,对象必须是可序列化的。有关详细信息,请参阅Session-State Modes