【发布时间】:2014-08-01 10:30:14
【问题描述】:
我有一个从 MySQL 数据库填充的 datagridview。 我正在循环通过它来验证信息,并在验证信息时将记录添加到数据表中。
我遇到的问题是我当前的代码只添加了第一条记录。 我希望它添加一条记录,继续,然后添加另一条记录并继续等等。
有什么想法吗?
Private Sub ProcessorWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles ProcessorWorker.DoWork
Console.WriteLine("ProcessorWorker Invoked...")
Try
Dim dt As New DataTable
Dim entryid As String = " "
For Each row As DataGridViewRow In orderslist.Rows
If row.Cells("member_id").Value.ToString IsNot Nothing Then
entryid = row.Cells("entry_id").Value.ToString
If entryid <> "" Then
Dim memberid As String = row.Cells("member_id").Value.ToString
Dim cn1 As New MySqlConnection
cn1.ConnectionString = ###myconnectionstring###
Dim vm_components As New MySqlDataAdapter("Select * FROM website_orders WHERE order_id= '" & entryid & "'", cn1)
Dim vm_components_table As New DataTable
vm_components.Fill(vm_components_table)
Dim row1 As DataRow
row1 = vm_components_table.Select("order_id = '" & entryid & "'").FirstOrDefault()
If Not row1 Is Nothing Then
If row1.Item("cart_id") IsNot Nothing Then web_cartid = row1.Item("cart_id").ToString
If row1.Item("email") IsNot Nothing Then web_email = row1.Item("email").ToString
If row1.Item("member_id") IsNot Nothing Then web_memberid = row1.Item("member_id").ToString
If row1.Item("firstname") IsNot Nothing Then web_firstname = row1.Item("firstname").ToString
If row1.Item("lastname") IsNot Nothing Then web_lastname = row1.Item("lastname").ToString
If row1.Item("company") IsNot Nothing Then web_company = row1.Item("company").ToString
If row1.Item("address1") IsNot Nothing Then web_address1 = row1.Item("address1").ToString
If row1.Item("address2") IsNot Nothing Then web_address2 = row1.Item("address2").ToString
If row1.Item("city") IsNot Nothing Then web_city = row1.Item("city").ToString
If row1.Item("postcode") IsNot Nothing Then web_postcode = row1.Item("postcode").ToString
End If
dt.Columns.Add("Product Ordered", Type.GetType("System.String"))
dt.Columns.Add("Operating System", Type.GetType("System.String"))
dt.Columns.Add("Company", Type.GetType("System.String"))
dt.Columns.Add("Customer", Type.GetType("System.String"))
dt.Columns.Add("Email Address", Type.GetType("System.String"))
WebOrders.DataSource = dt
Dim dr As DataRow
dr = dt.NewRow
dr("Product Ordered") = web_servertype
dr("Operating System") = web_os
dr("Company") = "WORLD"
dr("Customer") = web_firstname & " " & web_lastname
dr("Email Address") = web_email
dt.Rows.Add(dr)
WebOrders.AllowUserToAddRows = False
End If
End If
Next
Catch ex As Exception
End Try
End Sub
【问题讨论】:
-
我不知道为什么和/或谁删除了我的评论,但接受的答案应该解决以下问题:1)吞咽异常是一种不好的做法。 2)BackgroundWorker 有它自己的“错误处理”机制。 3) 跨线程异常的可能性。 4) 始终处置 IDisposable 对象。 5) 始终使用准备好的/参数化的 SQL。
标签: vb.net loops datagridview datatable