我有代码循环遍历数据表并将它们插入到数据库中。第一个循环中的数据插入正常,但它会重复循环的其余部分。所以我最终在db中重复了相同的数据。
Dim custID As Integer
Dim objDT As System.Data.DataTable
Dim objDR As System.Data.DataRow
Sub Page_Load
End Sub
'store customer info'
Sub btTest_Click(sender As Object, e As EventArgs)
Dim con As New System.Data.OleDb.OleDbConnection
Dim myPath As String
myPath = Server.MapPath("faraxday.mdb")
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data source=" & myPath & ";"
Dim myCommand As New System.Data.OleDb.OleDbCommand
myCommand.CommandText = "INSERT INTO customer(name, address_1, address_2, postcode, email, pass) values('" & name.Text & "','" & address_1.Text & "','" & address_2.Text & "','" & postcode.Text & "','" & email.Text & "','" & pass.Text & "')"
myCommand.Connection = con
con.Open()
myCommand.ExecuteNonQuery()
con.Close()
GetID()
recordOrder()
End Sub
'get customer id'
Function GetID() As System.Data.DataSet
Dim con As New System.Data.OleDb.OleDbConnection
Dim myPath As String
myPath = Server.MapPath("faraxday.mdb")
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data source=" & myPath & ";"
Dim adp As New System.Data.OleDb.OleDbDataAdapter("SELECT customer_id FROM customer ORDER BY customer_id DESC", con)
Dim dt As New System.Data.DataTable
adp.Fill(dt)
custID = dt.Rows(0).Item("customer_id")
End Function
'Add items to db'
Function recordOrder()
objDT = Session("Cart")
Dim intCounter As Integer
Dim con2 As New System.Data.OleDb.OleDbConnection
Dim myPath2 As String
myPath2 = Server.MapPath("faraxday.mdb")
con2.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data source=" & myPath2 & ";"
Dim myCommand2 As New System.Data.OleDb.OleDbCommand
Dim sql As String
myCommand2.Connection = con2
con2.Open()
'variables'
Dim order_date As String
Dim coupon_ID As String
Dim customer_id As String
Dim quantity As String
'variables'
Try
For Each objDR In objDT.Rows
order_date = System.DateTime.Now.Date
coupon_ID = objDR("ID")
quantity = objDR("quantity")
myCommand2.CommandText = "INSERT INTO orders(order_date, coupon_id, customer_id, quantity) VALUES ( @order_date ,@coupon_ID,@customer_id,@quantity)"
myCommand2.Parameters.Add("@order_date", order_date)
myCommand2.Parameters.Add("@coupon_ID", coupon_ID)
myCommand2.Parameters.Add("@customer_id", custID)
myCommand2.Parameters.Add("@quantity", quantity)
myCommand2.ExecuteNonQuery()
Next
Catch ex As Exception
objDT = Session("Cart")
test.Text = ex.toString()
Finally
If con2.State = ConnectionState.Open Then
con2.Close()
End If
End Try
End Function
答案 0 :(得分:0)
避免重复可能会在for循环中添加另一个过滤。
类似的东西:
For Each objDR In objDT.Rows
//Add some filter here
// "Select Unique ID from Table where Id=@Id"
// Load to DataSet
//Check if Exists
If(dt.Tables[0].Rows.Count > 0) THEN
//NOT SAVE
Else
//Save
order_date = System.DateTime.Now.Date
coupon_ID = objDR("ID")
quantity = objDR("quantity")
myCommand2.CommandText = "INSERT INTO orders(order_date, coupon_id, customer_id, quantity) VALUES ( @order_date ,@coupon_ID,@customer_id,@quantity)" myCommand2.Parameters.Add("@order_date", order_date)
myCommand2.Parameters.Add("@coupon_ID", coupon_ID)
myCommand2.Parameters.Add("@customer_id", custID)
myCommand2.Parameters.Add("@quantity", quantity)
myCommand2.ExecuteNonQuery()
END
Next
此致