如何使用数据库mysql与后台工作者vb.net将数据插入客户端上的服务器?

时间:2017-01-05 20:41:34

标签: mysql vb.net

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
                   koneksiserver()



            Try


                Dim sqlinsert As String = "INSERT INTO sentitems (TextDecoded, " & _
                                 "DestinationNumber,SenderID,ID)" & _
                                 "VALUES(@isi,@nohp,@modem,@ID)"
                Dim cmd = New MySqlCommand(sqlinsert, MyCon)
                cmd.Parameters.Add("@isi", MySqlDbType.Text)
                cmd.Parameters.Add("@nohp", MySqlDbType.VarChar)
                cmd.Parameters.Add("@modem", MySqlDbType.VarChar)
                cmd.Parameters.Add("@ID", MySqlDbType.Int16)




                For i As Integer = 0 To DataGridView3.Rows.Count - 0
                    cmd.Parameters("@isi").Value = DataGridView3.Rows(i).Cells(1).Value
                    cmd.Parameters("@nohp").Value = DataGridView3.Rows(i).Cells(3).Value
                    cmd.Parameters("@modem").Value = DataGridView3.Rows(i).Cells(4).Value
                    cmd.Parameters("@ID").Value = DataGridView3.Rows(i).Cells(6).Value
                    cmd.ExecuteNonQuery()

                Next
                DisplayStatus("Conected...")
                isExecuting = True

                Dim callback As New AsyncCallback(AddressOf HandleCallback)
                cmd.BeginExecuteNonQuery(callback, cmd)






                MyCon.Close()
            Catch ex As Exception






                'MyCon.Close()

                ' MsgBox("Export Berhasil", MsgBoxStyle.Exclamation, "DHAPU SMS")

            End Try
    End Sub
Private Sub set_db_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles set_db.Click
        BackgroundWorker1.RunWorkerAsync()









    End Sub

1 个答案:

答案 0 :(得分:0)

Azmy; 假设您使用BackgroundWorker来阻止UI锁定:

你不能引用绑定到UI线程的任何对象,引用DataGridView3,DisplayStatus和MsgBox都是有保证的例外。

也是你的循环          对于i As Integer = 0 To DataGridView3.Rows.Count - 0

将失败,因为您没有正确调整计数使用-1而不是减去0

要完成您想要的任务,您必须通过System.ComponentModel.DoWorkEventArgs参数传递表对象或数据对象数组或您希望更新的数据。