数据控制:
SqlConnection conn = db.GetConnection();
SqlTransaction trans = conn.BeginTransaction();
foreach(CartInfo cartItem in cartItems)
{
insertSp[1].Value=cartItem.bookid;
insertSp[2].Value=cartItem.price;
insertSp[3].Value=cartItem.itemname;
insertSp[4].Value=cartItem.quantity;
db.ExecuteNonQuery(trans,CommandType.StoredProcedure,"InsertCartItems",insertSp);
//不能将trans.Commit()放在foreach内部,因为commit会结束事务。
}
trans.Commit();