这就是我全部的操作代码:
Dim objDT As System.Data.DataTable
Dim objDR As System.Data.DataRow
Private Sub Page_Load(s As Object, e As EventArgs)
If Not IsPostBack Then
makeCart()
End If
End Sub
Function makeCart()
objDT = New System.Data.DataTable("Cart")
objDT.Columns.Add("ID", GetType(Integer))
objDT.Columns("ID").AutoIncrement = True
objDT.Columns("ID").AutoIncrementSeed = 1
objDT.Columns.Add("Quantity", GetType(Integer))
objDT.Columns.Add("Product", GetType(String))
Session("Cart") = objDT
End Function
Sub AddToCart(s As Object, e As EventArgs)
objDT = Session("Cart")
if objDT.rows.Count <3 then
objDR = objDT.NewRow
objDR("Quantity") = txtQuantity.Text
objDR("Product") = txtProduct.Text
objDT.Rows.Add(objDR)
else
Label5.Text = "sorry,you only allow to add three item to the cart at once"
end if
Session("Cart") = objDT
dg.DataSource = objDT
dg.DataBind()
txtQuantity.text = ""
txtProduct.text = ""
End Sub
Sub Delete_Item(s As Object, e As DataGridCommandEventArgs)
objDT = Session("Cart")
objDT.Rows(e.Item.ItemIndex).Delete()
Session("Cart") = objDT
dg.DataSource = objDT
dg.DataBind()
End Sub
Sub AddRecord(ByVal Sender As Object, ByVal E As EventArgs)
Dim objConn As New oleDbConnection
Dim sqlCmd as String
Dim objCmd As New OleDbCommand(sqlCmd,objConn)
Dim x as integer
objConn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;"
objConn.ConnectionString+= "Data Source= " & Server.MapPath("Account.mdb")
objDT = Session("Cart")
for x = 0 to objDT.Rows.Count -1
sqlCmd = "INSERT INTO ordered (Product, Quantity) VALUES ( @Product , @Quantity )"
objCmd = New OleDbCommand(sqlCmd, objConn)
objCmd.Parameters.Add("@Product", objDT.Rows(x)!Product.ToString())
objCmd.Parameters.Add("@Quantity",objDT.Rows(x)!Quantity.ToString())
next
Session("Cart") = objDT
objConn.Open()
objCmd.ExecuteNonQuery()
objConn.Close()
End Sub