⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shoppingcartdb.vb

📁 学习visual studio.net的资料!
💻 VB
📖 第 1 页 / 共 2 页
字号:
            ' Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure

            Dim parameterCartID As SQLParameter = New SQLParameter("@CartID", SQLDataType.NChar, 50)
            parameterCartID.Value = cartID
            myCommand.Parameters.Add(parameterCartID)
          
            ' Add Parameters to SPROC
            Dim parameterItemCount As SQLParameter = New SQLParameter("@ItemCount", SQLDataType.Int, 4)
            parameterItemCount.Direction = ParameterDirection.Output
            myCommand.Parameters.Add(parameterItemCount)

            Try 
                ' Open the connection and execute the Command
                myConnection.Open()
                myCommand.Execute()
            Catch e As Exception
                ' An error occurred, pass the exception up
                throw e
            Finally 
                ' Close the Connection
                If myConnection.State = DBObjectState.Open then
                    myConnection.Close()
                End If
            End Try

            ' Return the ItemCount (obtained as out paramter of SPROC)
            return CInt(parameterItemCount.Value)
        
        End Function
 
        '*******************************************************
        '
        ' ShoppingCartDB.GetTotal() Method <a name="GetTotal"></a>
        '
        ' The GetTotal method returns the total price of all 
        ' items within the shopping cart.
        '
        ' Other relevant sources:  
        '     + <a href="srcview.aspx?path=../ShoppingCart.src&file=docs/ShoppingCartTotal.htm" style="color:green">ShoppingCartTotal Stored Procedure</a>
        '
        '******************************************************* 
 
        Public Function GetTotal(cartID As String) As Double
            
            ' Create Instance of Connection and Command Object
            Dim myConnection As SQLConnection = New SQLConnection(ConferenceDB.ConnectionString)
            Dim myCommand As SQLCommand = New SQLCommand("ShoppingCartTotal", myConnection)
                        
            ' Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure

            ' Add Parameters to SPROC
            Dim parameterCartID As SQLParameter = New SQLParameter("@CartID", SQLDataType.NChar, 50)
            parameterCartID.Value = cartID
            myCommand.Parameters.Add(parameterCartID)
                      
            Dim parameterTotalCost As SQLParameter = New SQLParameter("@TotalCost", SQLDataType.Float, 8)
            parameterTotalCost.Direction = ParameterDirection.Output
            myCommand.Parameters.Add(parameterTotalCost)

            Try 
                ' Open the connection and execute the Command
                myConnection.Open()
                myCommand.Execute()
            Catch e As Exception
                ' An error occurred, pass the exception up
                throw e
            Finally 
                ' Close the Connection
                If myConnection.State = DBObjectState.Open then
                    myConnection.Close()
                End If
            End Try

            ' Return the Total
            return CDbl(parameterTotalCost.Value)
        
        End Function

        '*******************************************************
        '
        ' ShoppingCartDB.MigrateCart() Method <a name="MigrateCart"></a>
        '
        ' The MigrateCart method migrates the items from one
        ' cartId to another.  This is used during the login
        ' and/or registration process to transfer a user's 
        ' temporary cart items to a permanent account.
        '
        ' Other relevant sources:  
        '     + <a href="srcview.aspx?path=../Login.src&file=docs/ShoppingCartMigrate.htm" style="color:green">ShoppingCartMigrate Stored Procedure</a>
        '
        '******************************************************* 

        Public Sub MigrateCart(oldCartId As String, NewCartId As String) 

            ' Create Instance of Connection and Command Object
            Dim myConnection As SQLConnection = New SQLConnection(ConferenceDB.ConnectionString)
            Dim myCommand As SQLCommand = New SQLCommand("ShoppingCartMigrate", myConnection)

            ' Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure

            ' Add Parameters to SPROC
            Dim cart1 As SQLParameter = New SQLParameter("@OriginalCartId ", SQLDataType.NChar, 50)
            cart1.Value = oldCartId
            myCommand.Parameters.Add(cart1)
            
            Dim cart2 As SQLParameter= New SQLParameter("@NewCartId ", SQLDataType.NChar, 50)
            cart2.Value = NewCartId
            myCommand.Parameters.Add(cart2)

            Try 
                ' Open the connection and execute the Command
                myConnection.Open()
                myCommand.Execute()
            Catch e As Exception
                ' An error occurred, pass the exception up
                throw e
            Finally 
                ' Close the Connection
                If myConnection.State = DBObjectState.Open then
                    myConnection.Close()
                End If
            End Try

        End Sub

        '*******************************************************
        '
        ' ShoppingCartDB.EmptyCart() Method <a name="EmptyCart"></a>
        '
        ' The EmptyCart method removes all current items within
        ' the shopping cart.
        '
        ' Other relevant sources:  
        '     + <a href="srcview.aspx?path=../ShoppingCart.src&file=docs/ShoppingCartEmpty.htm" style="color:green">ShoppingCartEmpty Stored Procedure</a>
        '
        '******************************************************* 

        Public Sub EmptyCart(cartID As String) 

            ' Create Instance of Connection and Command Object
            Dim myConnection As SQLConnection = New SQLConnection(ConferenceDB.ConnectionString)
            Dim myCommand As SQLCommand = New SQLCommand("ShoppingCartEmpty", myConnection)

            ' Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure

            ' Add Parameters to SPROC
            Dim mycartid As SQLParameter = New SQLParameter("@CartID", SQLDataType.NChar, 50)
            mycartid.Value = cartID
            myCommand.Parameters.Add(mycartid)

            Try 
                ' Open the connection and execute the Command
                myConnection.Open()
                myCommand.Execute()
            Catch e As Exception
                ' An error occurred, pass the exception up
                throw e
            Finally 
                ' Close the Connection
                If myConnection.State = DBObjectState.Open then
                    myConnection.Close()
                End If
            End Try
            
        End Sub


        '*******************************************************
        '
        ' ShoppingCartDB.GetShoppingCartId() Method <a name="GetShoppingCartId"></a>
        '
        ' The GetShoppingCartId method is used to calculate the
        ' "ShoppingCart" ID key used for a tracking a browser.
        '
        ' The ShoppingCartID value is either the User's Identity
        ' Name (if they are a registered and authenticated user),
        ' or a random GUID calculated for guest visitors or 
        ' customers who have not yet logged in.
        '
        '******************************************************* 
        
        Public Function GetShoppingCartId() As String

            ' Obtain current HttpContext of ASP+ Request
            Dim context as System.Web.HttpContext = System.Web.HttpContext.Current

            ' If the user is authenticated, use their customerId as a permanent shopping cart id 
            If context.User.Identity.Name <> "" Then
                return context.User.Identity.Name
            End If
            
            ' If user is not authenticated, either fetch (or issue) a new temporary cartID
            If context.Request.Cookies("Conference_CartID") <> Nothing Then
                return context.Request.Cookies("Conference_CartID").Value
            
            Else
                ' Generate a new random GUID using System.Guid Class
                Dim tempCartId As Guid = Guid.NewGuid()
                
                ' Send tempCartId back to client as a cookie
                context.Response.Cookies("Conference_CartID").Value = tempCartId.ToString()
                
                ' Return tempCartId
                return tempCartId.ToString()
            End If
            
        End Function 
        
    End Class

End Namespace

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -