如何在会话中存储多个值

如何在会话中存储多个值

本文介绍了如何在会话中存储多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,在其中我从数据库中检索数据。我将数据存储在会话变量中并显示数据,但会话只显示一条记录。但在数据库中包含10条记录。我希望会话应显示10条记录。



i am creating an application in which i retrive data from database. i am store data in session variable and display a data but session display only one record. but in database contains 10 record . i want session should display 10 record.

protected void Page_Load(object sender, EventArgs e)
   {
       SqlConnection con = new SqlConnection("Data Source=Aarambh;Initial Catalog=rebuild_technology;Integrated Security=True");
       SqlCommand cmd = new SqlCommand("select * from content_managment ", con);
       DataSet ds = new DataSet("temp");
       SqlDataAdapter ad = new SqlDataAdapter();

       ad.SelectCommand = cmd;
       ad.Fill(ds);
      // DataRow dr = ds.Tables[0].Rows[0];
       Session["divhtml_heading1"] = ds.Tables[0].Rows[0];
      // Session["divhtml_heading2"] = dr["divhtml_content"].ToString();

   }

推荐答案

Session["SessionName"] = dt; // dt is data table

// get data like this

DataTable Dtbl= (DataTable)Session["SessionName"];


Session["divhtml_heading1"] = ds.Tables[0];
// Later to retrieve it
DataTable dt = (DataTable)Session["divhtml_heading1"];





参考:

[]


SqlCommand cmd1 = new SqlCommand("select * from content_managment ", con);


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


    }
}
Session["divhtml_heading1"] = yourList;
dr1.Close();
cmd1.Dispose();


这篇关于如何在会话中存储多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 16:47