保存列表到 ASP.NET 中的会话

发布于 2024-10-30 20:24:23 字数 1692 浏览 3 评论 0原文

购物车项目保存在 SQL 数据库中。

我想将所有 CartItems 放在一个 List 中并转移到 Instance.Items。

实例变量正在保存到会话中。

代码如下。

public class ShoppingCart
{
    public List<CartItem> Items { get; private set; }
    public static SqlConnection conn = new SqlConnection(connStr.connString);
    public static readonly ShoppingCart Instance;

    static ShoppingCart()
    {
        if (HttpContext.Current.Session["ASPNETShoppingCart"] == null)
        {
                Instance = new ShoppingCart();
                Instance.Items = new List<CartItem>();
                HttpContext.Current.Session["ASPNETShoppingCart"] = Instance;  
        }
        else
        {
            Instance = (ShoppingCart)HttpContext.Current.Session["ASPNETShoppingCart"];
        }
    }

返回 List 的代码。我想将从该函数返回的列表保存到 Instance.Items。这样就可以保存到session中了。

    public static List<CartItem> loadCart(String CustomerId)
    {
        String sql = "Select * from Cart where CustomerId='" + CustomerId + "'";
        SqlCommand cmd = new SqlCommand(sql, conn);
        List<CartItem> lstCart = new List<CartItem>();
        try
        {
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                CartItem itm = new CartItem(Convert.ToInt32(reader["ProductId"].ToString()));
                itm.Quantity = Convert.ToInt32(reader["Quantity"].ToString());
                lstCart.Add(itm);
            }

        }
        catch (Exception ex)
        { }
        finally
        {
            conn.Close();
        }

        return lstCart;
    }

CartItems are saved in the the SQL database.

I want to put all CartItems in a List and transfer to Instance.Items.

The Instance Variable is being saved into the session.

The code is below.

public class ShoppingCart
{
    public List<CartItem> Items { get; private set; }
    public static SqlConnection conn = new SqlConnection(connStr.connString);
    public static readonly ShoppingCart Instance;

    static ShoppingCart()
    {
        if (HttpContext.Current.Session["ASPNETShoppingCart"] == null)
        {
                Instance = new ShoppingCart();
                Instance.Items = new List<CartItem>();
                HttpContext.Current.Session["ASPNETShoppingCart"] = Instance;  
        }
        else
        {
            Instance = (ShoppingCart)HttpContext.Current.Session["ASPNETShoppingCart"];
        }
    }

The code that returns List . I want to save the List that is being returned from this function to Instance.Items. So that it can be saved into the session.

    public static List<CartItem> loadCart(String CustomerId)
    {
        String sql = "Select * from Cart where CustomerId='" + CustomerId + "'";
        SqlCommand cmd = new SqlCommand(sql, conn);
        List<CartItem> lstCart = new List<CartItem>();
        try
        {
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                CartItem itm = new CartItem(Convert.ToInt32(reader["ProductId"].ToString()));
                itm.Quantity = Convert.ToInt32(reader["Quantity"].ToString());
                lstCart.Add(itm);
            }

        }
        catch (Exception ex)
        { }
        finally
        {
            conn.Close();
        }

        return lstCart;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霞映澄塘 2024-11-06 20:24:23

如果您将整个对象提交给会话,则这些项目应与该对象一起存储在会话中。

为什么不做这样的事情呢?:

public class ShoppingCart
{
    public List<CartItem> Items { get; private set; }
    public static SqlConnection conn = new SqlConnection(connStr.connString);
    public static readonly ShoppingCart Instance;

    static ShoppingCart RetrieveShoppingCart()
    {
        if (HttpContext.Current.Session["ASPNETShoppingCart"] == null)
        {
                Instance = new ShoppingCart();
                Instance.Items = new List<CartItem>();
                HttpContext.Current.Session["ASPNETShoppingCart"] = Instance;  
        }
        else
        {
            Instance = (ShoppingCart)HttpContext.Current.Session["ASPNETShoppingCart"];
        }

        return Instance;
    }
}

If you're committing the entire object to session, the items should be stored in session with the object.

Why not do something like this instead?:

public class ShoppingCart
{
    public List<CartItem> Items { get; private set; }
    public static SqlConnection conn = new SqlConnection(connStr.connString);
    public static readonly ShoppingCart Instance;

    static ShoppingCart RetrieveShoppingCart()
    {
        if (HttpContext.Current.Session["ASPNETShoppingCart"] == null)
        {
                Instance = new ShoppingCart();
                Instance.Items = new List<CartItem>();
                HttpContext.Current.Session["ASPNETShoppingCart"] = Instance;  
        }
        else
        {
            Instance = (ShoppingCart)HttpContext.Current.Session["ASPNETShoppingCart"];
        }

        return Instance;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文