📄 shoppingcart.vb
字号:
'********************************************************************************
'这是用来处理购物车的
'ShoppingCart 类,专门用来处理有关购物车的操作。这里是将书的BookId和购书数量BookNum保存到了HashTable中,然后将其保存到Session中。
'********************************************************************************
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Configuration '因为用到了ConfigurationSettings类
Imports System.Collections '因为用到了HashTable
Imports System.Web
Imports Microsoft.VisualBasic '因为用到了Now函数
NameSpace nsShop 'nsShop为自己定义的名称空间的名称
'该类用来查询、添加、删除和更新记录
Public Class ShoppingCart 'ShoppingCart是自己定义的类的名称
'定义一个只读属性,用来返回购物车中是否有物品
Public ReadOnly Property HasBooks As Boolean
Get
'从Session中获取购物车中的货物信息
Dim htBooks As HashTable=HttpContext.Current.Session("Books")
If htBooks.Count>0 Then
Return(True)
Else
Return(False)
End If
End Get
End Property
'定义一个只读属性,用来返回购物总数量
Public ReadOnly Property TotalNum As Integer
Get
'从Session中获取购物车中的货物信息
Dim htBooks As HashTable=HttpContext.Current.Session("Books")
'将购物车中的所有货物数量加起来
Dim intTotalNum As Integer=0
Dim objItem As DictionaryEntry
For Each objItem In htBooks
intTotalNum = intTotalNum + objItem.Value
Next
Return(intTotalNum)
End Get
End Property
'定义一个只读属性,用来返回购物总金额
Public ReadOnly Property TotalMoney As Single
Get
'从Session中获取购物车中的货物信息
Dim htBooks As HashTable=HttpContext.Current.Session("Books")
'将购物车中的所有货物数量加起来
Dim sglTotalMoney As Single=0
Dim objItem As DictionaryEntry
For Each objItem In htBooks
'此处会掉用函数返回价格,然后计算出总金额
sglTotalMoney = sglTotalMoney + GetBookPrice(objItem.Key) * objItem.Value
Next
Return(sglTotalMoney)
End Get
End Property
'定义一个私有函数,专门用来返回每一本书的价格
Private Function GetBookPrice(intBookId As Integer) As Single
Dim conn As New OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
Dim strSql As String="Select BookPrice From Book Where BookId=" & intBookId
Dim cmd As New OleDbCommand(strSql, conn) '建立Command对象
conn.Open
Dim sglBookPrice As Single=cmd.ExecuteScalar() '返回第一行第一列的记录值,也就是价格
conn.Close
Return(sglBookPrice) '返回函数值
End Function
'定义一个私有函数,专门用来返回每一本书的名称
Private Function GetBookName(intBookId As Integer) As String
Dim conn As New OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
Dim strSql As String="Select BookName From Book Where BookId=" & intBookId
Dim cmd As New OleDbCommand(strSql, conn) '建立Command对象
conn.Open
Dim strBookName As String=cmd.ExecuteScalar() '返回第一行第一列的记录值,也就是价格
conn.Close
Return(strBookName) '返回函数值
End Function
'建立构造函数,在其中会判断,如果购物车为空,就会初始化
Public Sub New()
If HttpContext.Current.Session("Books") Is Nothing Then
Dim htBooks As New HashTable()
HttpContext.Current.Session("Books")=htBooks
End If
End Sub
'该过程用来返回购物车中的所有书籍,注意,这里是将HashTable中的数据转换到了一个DataTable中,最后返回一个DataView对象
'转换成DataTable的原因是为了显示购物车时方便的帮定到DataGrid控件
Public Function GetBook() As DataView
'从Session中获取购物车中的货物信息
Dim htBooks As HashTable=HttpContext.Current.Session("Books")
'下面将这个HashTable转换为DataTable
Dim dt As New DataTable("Books") '声明并建立DataTable对象,表的名称为 Books
Dim dc As DataColumn '声明DataColumn对象变量
Dim dr As DataRow '声明DataRow对象变量
'下面开始建立表格的结构
'建立第0列,书编号BookId
dc=New DataColumn("BookId",System.Type.GetType("System.Int32"))
dt.Columns.Add(dc)
'建立第1列,书名称BookName
dc=New DataColumn("BookName",System.Type.GetType("System.String"))
dt.Columns.Add(dc)
'建立第2列,书单价BookPrice
dc=New DataColumn("BookPrice",System.Type.GetType("System.Single"))
dt.Columns.Add(dc)
'建立第3列,购书数量BookNum
dc=New DataColumn("BookNum",System.Type.GetType("System.Int32"))
dt.Columns.Add(dc)
'建立第4列,这是一个计算列,为每本书的价格 * 购书数量
dc=New DataColumn("SubTotal",System.Type.GetType("System.Single"),"BookPrice*BookNum")
dt.Columns.Add(dc)
'下面根据HashTable中的购书信息添加每一行
Dim objItem As DictionaryEntry
For Each objItem In htBooks
'逐行赋值
dr=dt.NewRow() '新建1行,注意和新建列的区别
dr("BookId")=objItem.Key '书的编号
dr("BookName")=GetBookName(objItem.Key) '调用函数,返回书的名称
dr("BookPrice")=GetBookPrice(objItem.Key) '调用函数,返回书的价格
dr("BookNum")=objItem.Value '购书数量
dt.Rows.Add(dr) '将该行加到表dt中
Next
'返回函数值,注意返回的是DataView
Return(dt.DefaultView)
End Function
'该函数用来判断该书是否已经在购物车中
Public Function IsExist(intBookId As String) As Boolean
'从Session中获取购物车中的货物信息
Dim htBooks As HashTable=HttpContext.Current.Session("Books")
If htBooks.Contains(intBookId)=True Then
Return(True) '表示该书已经存在
Else
Return(False) '表示该书不存在
End If
End Function
'向购物车中添加一本书,如果该书已经存在,只要修改数量即可,如果不存在,则需要添加一项
Public Sub AddBook(intBookId As Integer)
'从Session中获取购物车中的购物信息
Dim htBooks As HashTable=HttpContext.Current.Session("Books")
If htBooks.Contains(intBookId)=True Then
'这表示该书已经存在,只要将数量加上1即可
htBooks(intBookId) = htBooks(intBookId) + 1
Else
'表示该书不存在,需要添加一项。其中第二个参数1表示添加1本
htBooks.Add(intBookId,1)
End If
'最后将新的购物信息保存到Session中
HttpContext.Current.Session("Books")=htBooks
End Sub
'从购物车中删除一本书
Public Sub RemoveBook(intBookId As Integer)
'从Session中获取购物车中的购物信息
Dim htBooks As HashTable=HttpContext.Current.Session("Books")
'下面删除该书
htBooks.Remove(intBookId)
'最后将新的购物信息保存到Session中
HttpContext.Current.Session("Books")=htBooks
End Sub
'更改购物车中的货物数量
Public Sub UpdateBook(intBookId As Integer,intBookNum As Integer)
'从Session中获取购物车中的购物信息
Dim htBooks As HashTable=HttpContext.Current.Session("Books")
'下面更改数量
htBooks(intBookId)=intBookNum '注意,这里如果该书存在,就会更改数量,如果不存在,会自动添加一项
If intBookNum<=0 Then
htBooks.Remove(intBookId) '如果数量<=0,就将其删除
End If
'最后将新的购物信息保存到Session中
HttpContext.Current.Session("Books")=htBooks
End Sub
'结账函数。该函数将产生本次订单,并记录明细情况,最后返回订单号码
Public Function CheckOut() As Integer
'因为多次用到Command对象,所以这里建立Command对象的方式不太一样,请大家注意
Dim conn As New OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
Dim cmd As New OleDbCommand()
cmd.Connection=conn '指定Command对象的连接属性
cmd.CommandType=CommandType.Text '指定Command对象的命令为SQL字符串
'下面打开连接
conn.Open
'下面首先添加一个订单,注意这里要调用属性总数量和总金额
cmd.CommandText="Insert Into Orders(UserId,TotalNum,TotalMoney,SubmitDate) Values('" & HttpContext.Current.Session("UserId") & "'," & TotalNum & "," & TotalMoney & ",#" & Now() & "#)"
cmd.ExecuteNonquery()
'下面马上返回本次的订单编号
cmd.CommandText="Select Top 1 OrderId From Orders Where UserId='" & HttpContext.Current.Session("UserId") & "' Order By OrderId Desc"
Dim intOrderId As Integer=cmd.ExecuteScalar() '返回第一行第一列的记录值,也就是订单号码
'下面从Session中获取购物信息,并依次添加到订单详细表中
Dim htBooks As HashTable=HttpContext.Current.Session("Books")
Dim objItem As DictionaryEntry
For Each objItem In htBooks
cmd.CommandText="Insert Into Orders_Particular(OrderId,BookId,BookNum) Values(" & intOrderId & "," & objItem.Key & "," & objItem.Value & ")"
cmd.ExecuteNonquery()
Next
'下面要更新Users表中该用户的购买金额和购买数量
cmd.CommandText="Update Users Set TotalNum = TotalNum + " & TotalNum & ",TotalMoney = TotalMoney + " & TotalMoney & " Where UserId='" & HttpContext.Current.Session("UserId") & "'"
cmd.ExecuteNonquery()
'下面关闭数据库连接
Conn.Close
'结账完毕,可以将购物车清空
htBooks.Clear '清空所有购物信息
HttpContext.Current.Session("Books")=htBooks '将清空的HashTable重新保存到Session中,以备再次购物
'返回函数值,就是产生的订单号
Return(intOrderId)
End Function
End Class
End NameSpace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -