一旦已经将数据写入到Orders表中需要创建一个订单在其中写入OrderLines表这包含订单ID产品ID数量和价格在这之后提交事务
change the query and parameters for the order lines
cmdCommandText = INSERT INTO OrderLines(OrderID ProductID Quantity Price) & _
VALUES (@OrderID @ProductID @Quantity @Price)
cmdParametersClear()
cmdParametersAdd(@OrderID DataSqlDbTypeInt)
cmdParametersAdd(@ProductID DataSqlDbTypeInt)
cmdParametersAdd(@Quantity DataSqlDbTypeInt)
cmdParametersAdd(@Price DataSqlDbTypeMoney)
cmdParameters(@OrderID)Value = OrderID
For Each item As CartItem In ProfileCartItems
cmdParameters(@ProductID)Value = itemProductID
cmdParameters(@Quantity)Value = itemQuantity
cmdParameters(@Price)Value = itemPrice
cmdExecuteNonQuery()
Next
clear the cart
ProfileCartItemsClear()
commit the transaction
trans
Commit()
下一部分是异常处理程序如果有任何类型的数据库错误则必须回滚异常并且将其写入到错误日志第章中将更详细地介绍异常处理下面的代码专门适用于处理SQL错误并且将造成应用程序中的错误
Catch Sqlex As Exception
some form of error rollback the transaction
and rethrow the exception
If trans IsNot Nothing Then
transRollback()
End If
Log the exception
Toolslog(An error occurred while creating the order SqlEx)
Throw New Exception(An error occurred while creating the order SqlEx)
Return
[] [] [] [] [] [] [] []