在三层架构中使用事务处理
来源:互联网 发布:java实现三个人打球 编辑:程序博客网 时间:2024/05/01 15:42
使用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);
}
/// 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);
}
- 在三层架构中使用事务处理
- 事务处理在三层架构中的应用
- 在三层架构中使用ObjectDataSource
- 在ASP.net中使用三层架构使用存储过程
- 如何在ASP.NET中使用三层架构
- 如何在ASP.NET中使用三层架构
- Asp.net 在三层架构中事务的使用
- 在winform中使用三层架构学习总结
- 在jsp中使用事务处理
- 在WCF中使用事务处理
- Entity Framework在三层架构中的使用
- [ASP中使用三层架构] 1.前言
- [ASP中使用三层架构] 2.目录
- 三层架构中web.config配置文件使用
- 三层架构中多层的使用关系
- Oraclee 事务处理以及在c#中使用oracle的事务处理
- 在ASP.NET三层架构中使用泛型获取实体数据
- C#回顾学习笔记四十一:在三层架构中使用事务
- 获取本机名
- 两个程序看指针
- linux 启动全接触
- 在C#程序中使用系统热键
- Java与XML读写之DOM篇
- 在三层架构中使用事务处理
- 无题
- 解决Oracle XDB与Tomcat等的8080端口的冲突
- 三度来滁州!
- HP Openvms education training
- oracle decode nvl nvl2 sign(即sin) 函数用法
- Java与XML读写之SAX篇
- Java基本教学:线程入门
- 怎样在linux下卸载源代码版本的mysql?