簡體   English   中英

將多個值存儲在會話變量中

[英]Store multiple values in a session variable

在我提出問題之前,請考慮以下情況:我網站上的用戶有一個profileID 有一些pluginID與此profileID相關聯。

例如: User1可能有2個,3個和5個插件與他的個人資料相關聯。

當用戶登錄時,我將用戶的profileID存儲在會話變量cod 在某個頁面上,用戶嘗試編輯與其個人資料相關聯的plugins 所以,在那個頁面上,我必須從數據庫中檢索那些pluginID

我已應用此代碼,但pluginID從DB獲取最大的pluginID而不是所有的pluginID

SqlCommand cmd1 = new SqlCommand("select plugin_id from profiles_plugins where id=(select id from profiles_plugins where profile_id=" + Convert.ToInt32(Session["cod"]) + ")", con);
SqlDataReader dr1 = cmd1.ExecuteReader();
if (dr1.HasRows)
{
  while (dr1.Read())
  {
    Session["edp1"] = Convert.ToInt32(dr1[0]);
  }
}
dr1.Close();
cmd1.Dispose();

我試圖找出如何在此會話變量中存儲多個pluginID

謝謝

首先將值讀取到列表或數組,然后存儲列表或數組:

using(SqlCommand cmd1 = new SqlCommand("select plugin_id from profiles_plugins where id=(select id from profiles_plugins where profile_id=" + Convert.ToInt32(Session["cod"]) + ")", con))
{

    SqlDataReader dr1 = cmd1.ExecuteReader();
    var yourList = new List<int>();
    if (dr1.HasRows)
    {
        while (dr1.Read())
        {
           yourList.Add(Convert.ToInt32(dr1[0]));
        }
    }
    Session["edp1"] = yourList;
    dr1.Close();
}

另外,請閱讀Marc Gravell對您問題的評論。

您可以在會話密鑰下存儲您喜歡的任何對象,而不僅僅是標量值。 因此,只需定義包含您要存儲的所有值的自定義類型,創建此類型的實例並將其放入會話中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM