熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ASP.NET入門教程 13.4.5 計劃結賬[7]

2022-06-13   來源: .NET編程 

    一旦已經將數據寫入到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

            transCommit()

    下一部分是異常處理程序如果有任何類型的數據庫錯誤則必須回滾異常並且將其寫入到錯誤日志章中將更詳細地介紹異常處理下面的代碼專門適用於處理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

[]  []  []  []  []  []  []  []  


From:http://tw.wingwit.com/Article/program/net/201311/15648.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.