在三层架构中使用事务处理

使用PetShop sqlHelp

  /// <summary>
        
/// Update shopping cart for current user
        
/// </summary>
        
/// <param name="uniqueID">User id</param>
        
/// <param name="cartItems">Collection of shopping cart items</param>
        
/// <param name="isShoppingCart">Shopping cart flag</param>

         public   void  SetCartItems( 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{

                
// update cart using SqlTransaction
                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.ConnectionStringProfile);
                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.Type;
                        parms2[
5].Value = cartItem.Price;
                        parms2[
6].Value = cartItem.CategoryId;
                        parms2[
7].Value = cartItem.ProductId;
                        parms2[
8].Value = cartItem.Quantity;
                        SqlHelper.ExecuteNonQuery(trans, CommandType.Text, sqlInsert, parms2);
                    }

                    trans.Commit();
                }

                
catch (Exception e) {
                    trans.Rollback();
                    
throw new ApplicationException(e.Message);
                }

                
finally {
                    conn.Close();
                }

            }

            
else 
                
// delete cart
                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringProfile, CommandType.Text, sqlDelete, parms1);
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值