⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bookshopprofileprovider.cs

📁 一个非常好的网上书店系统
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Text;
using System.Configuration;
using System.Web;
using System.Web.Profile;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
//自定义配置文件提供程序
public sealed class BookShopProfileProvider : ProfileProvider
{

    //// 获取数据操作配置类
    private static readonly SqlBookShopProfileProvider dal = new SqlBookShopProfileProvider();

    // 内部变量
    private const string ERR_INVALID_PARAMETER = "Invalid Profile parameter:";
    private const string PROFILE_SHOPPINGCART = "ShoppingCart";
    private const string PROFILE_ACCOUNT = "AccountInfo";
    private static string applicationName = "BookShop";

    /// <summary>
    /// 应用程序名
    /// </summary>
    public override string ApplicationName
    {
        get
        {
            return applicationName;
        }
        set
        {
            applicationName = value;
        }
    }

    /// <summary>
    /// 初始化提供程序
    /// </summary>
    /// <param name="name">提供程序的名称</param>
    /// <param name="config">提供程序的配置</param>
    public override void Initialize(string name, NameValueCollection config)
    {
        if (config == null)
            throw new ArgumentNullException("config");
        if (string.IsNullOrEmpty(config["description"]))
        {
            config.Remove("description");
            config.Add("description", "BookShop Custom Profile Provider");
        }
        if (string.IsNullOrEmpty(name))
            name = "ShoppingCartProvider";

        if (config["applicationName"] != null && !string.IsNullOrEmpty(config["applicationName"].Trim()))
            applicationName = config["applicationName"];
        base.Initialize(name, config);
    }

    /// <summary>
    /// 返回指定应用程序实例的设置属性值集合和设置属性组。
    /// </summary>
    /// <param name="context">当前应用程序上下文</param>
    /// <param name="collection">一个系统的配置集合</param>
    /// <returns>返回一个当前的配置集合</returns>
    public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
    {
        //获取登录用户信息
        string username = (string)context["UserName"];
        bool isAuthenticated = (bool)context["IsAuthenticated"];

        SettingsPropertyValueCollection svc = new SettingsPropertyValueCollection();
        //遍历集合中的属性
        foreach (SettingsProperty prop in collection)
        {
            SettingsPropertyValue pv = new SettingsPropertyValue(prop);
            //判断属性名称
            //本实例涉及到两个属性的保存:购物篮和帐户地址
            switch (pv.Property.Name)
            {
                case PROFILE_SHOPPINGCART:
                    pv.PropertyValue = GetCartItems(username, true);
                    break;
                case PROFILE_ACCOUNT:
                    pv.PropertyValue = GetAccountInfo(username);
                    break;
                default:
                    throw new ApplicationException(ERR_INVALID_PARAMETER + " name.");
            }
            svc.Add(pv);
        }
        return svc;
    }

    /// <summary>
    /// 设置指定的属性设置组的值。
    /// </summary>
    /// <param name="context">当前应用程序上下文</param>
    /// <param name="collection">一个系统的配置集合.</param>
    public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
    {
        //判断当前登录用户
        string username = (string)context["UserName"];
        CheckUserName(username);
        bool isAuthenticated = (bool)context["IsAuthenticated"];
        int uniqueID = dal.GetUniqueID(username, isAuthenticated, false, ApplicationName);
        if (uniqueID == 0)
            uniqueID = dal.CreateProfileForUser(username, isAuthenticated, ApplicationName);

        //遍历用户的保存信息
        foreach (SettingsPropertyValue pv in collection)
        {
            if (pv.PropertyValue != null)
            {
                switch (pv.Property.Name)
                {
                    case PROFILE_SHOPPINGCART:
                        SetCartItems(uniqueID, (CartItem)pv.PropertyValue, true);
                        break;
                    case PROFILE_ACCOUNT:
                            SetAccountInfo(uniqueID, (AddressInfo)pv.PropertyValue);
                        break;
                    default:
                        throw new ApplicationException(ERR_INVALID_PARAMETER + " name.");
                }
            }
        }
        //更新配置时间等信息
        UpdateActivityDates(username, false);
    }

    // 获取profile属性

    // 获取帐户信息
    private static AddressInfo GetAccountInfo(string username)
    {
        return dal.GetAccountInfo(username, applicationName);
    }
    // 获取购物篮信息
    private static CartItem GetCartItems(string username, bool isShoppingCart)
    {
        //初始化一个购物篮实体操作类
        CartItem cart = new CartItem();
        foreach (CartItemInfo cartItem in dal.GetCartItems(username, applicationName, isShoppingCart))
        {
            //在购物篮中添加商品信息
            cart.Add(cartItem);
        }
        return cart;
    }
    // 更新帐户地址
    private static void SetAccountInfo(int uniqueID, AddressInfo addressInfo)
    {
        dal.SetAccountInfo(uniqueID, addressInfo);
    }
    // 更新购物篮信息
    private static void SetCartItems(int uniqueID, CartItem cart, bool isShoppingCart)
    {
        dal.SetCartItems(uniqueID, cart.CartItems, isShoppingCart);
    }
    /// <summary>
    /// 更新当前用户的购物篮信息
    /// </summary>
    /// <param name="uniqueID">用户ID</param>
    /// <param name="cartItems">购物篮中的商品</param>
    /// <param name="isShoppingCart">购物篮标志</param>
    private void SetCartItemsProfile(int uniqueID, ICollection<CartItemInfo> cartItems, bool isShoppingCart)
    {
        string sqlDelete = "DELETE FROM Cart WHERE UniqueID = @UniqueID AND IsShoppingCart = @IsShoppingCart;";

        SqlParameter[] parms1 = {				   
			new SqlParameter("@UniqueID", SqlDbType.Int),
			new SqlParameter("@IsShoppingCart", SqlDbType.Bit)};
        parms1[0].Value = uniqueID;
        parms1[1].Value = isShoppingCart;

        if (cartItems.Count > 0)
        {

            // 通过SQL事务更新信息
            string sqlInsert = "INSERT INTO Cart (UniqueID, ItemId, Name, Type, Price, CategoryId, ProductId, IsShoppingCart, Quantity) VALUES (@UniqueID, @ItemId, @Name, @Type, @Price, @CategoryId, @ProductId, @IsShoppingCart, @Quantity);";
            //定义购物篮的参数
            SqlParameter[] parms2 = {				   
			new SqlParameter("@UniqueID", SqlDbType.Int),	
			new SqlParameter("@IsShoppingCart", SqlDbType.Bit),
			new SqlParameter("@ItemId", SqlDbType.VarChar, 10),
			new SqlParameter("@Name", SqlDbType.VarChar, 80),
			new SqlParameter("@Type", SqlDbType.VarChar, 80),
			new SqlParameter("@Price", SqlDbType.Decimal, 8),
			new SqlParameter("@CategoryId", SqlDbType.VarChar, 10),
			new SqlParameter("@ProductId", SqlDbType.VarChar, 10),
			new SqlParameter("@Quantity", SqlDbType.Int)};
            parms2[0].Value = uniqueID;
            parms2[1].Value = isShoppingCart;
            SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);
            conn.Open();
            //开始事务
            SqlTransaction trans = conn.BeginTransaction(IsolationLevel.ReadCommitted);
            try
            {
                //在购物篮中添加信息
                SqlHelper.ExecuteNonQuery(trans, CommandType.Text, sqlDelete, parms1);
                foreach (CartItemInfo cartItem in cartItems)
                {
                    parms2[2].Value = cartItem.ItemId;
                    parms2[3].Value = cartItem.Name;
                    parms2[4].Value = cartItem.Price;
                    parms2[5].Value = cartItem.SupplierId;
                    parms2[6].Value = cartItem.ProductId;
                    parms2[7].Value = cartItem.Quantity;
                    SqlHelper.ExecuteNonQuery(trans, CommandType.Text, sqlInsert, parms2);
                }
                //执行事务

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -