简体   繁体   中英

how can i execute this code faster?

in my Billing form, i have a button to insert the datagridview data into two access databases. i used this code to insert to first database:

    Private Sub inserttotblbill()
    Dim billcon As OleDbConnection = New OleDbConnection(constr)
    Dim billcmd As New OleDbCommand

    For i = 0 To dgv.Rows.Count - 1
        billcon.Open()
        billcmd.Connection = billcon
        billcmd.CommandText = ("insert into tblbill(inum,snum,idate,cname,iname,iprc,iqnt,ipaid,itotal,iuser,itype) " _
                & " values('" _
                & TextBox1.Text _
                & "','" _
                & TextBox6.Text _
                & "','" _
                & TextBox2.Text _
                & "','" _
                & TextBox3.Text _
                & "','" _
                & dgv.Rows(i).Cells(0).Value _
                & "','" _
                & dgv.Rows(i).Cells(1).Value _
                & "','" _
                & dgv.Rows(i).Cells(2).Value _
                & "','" _
                & TextBox4.Text _
                & "','" _
                & dgv.Rows(i).Cells(3).Value _
                & "','" _
                & username _
                & "','" _
                & Label2.Text _
                & "')")
        If Not ListBox1.Items.Contains(dgv.Rows(i).Cells(4).Value) Then
            ListBox1.Items.Add(dgv.Rows(i).Cells(4).Value)
        End If
        billcmd.ExecuteNonQuery()
        billcon.Close()
    Next i
  end sub

i used this code to insert to second database:

    Private Sub inserttoreport()
    Dim rptcon As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Application.StartupPath & "\report.mdb; Jet OLEDB:Database Password=KNOZ1003")
    Dim rptcmd As New OleDbCommand

    For k = 0 To dgv.Rows.Count - 1
        rptcon.Open()
        rptcmd.Connection = rptcon
        If dgv.Rows(k).Cells(4).Value = "101" Then
            rptcmd.CommandText = ("insert into tab1(f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13) " _
            & " values('" _
            & TextBox1.Text _
            & "','" _
            & TextBox6.Text _
            & "','" _
            & TextBox2.Text _
            & "','" _
            & TextBox3.Text _
            & "','" _
            & dgv.Rows(k).Cells(0).Value _
            & "','" _
            & dgv.Rows(k).Cells(1).Value _
            & "','" _
            & dgv.Rows(k).Cells(2).Value _
            & "','" _
            & TextBox4.Text _
            & "','" _
            & dgv.Rows(k).Cells(3).Value _
            & "','" _
            & TextBox5.Text _
            & "','" _
            & Label2.Text _
            & "','" _
            & username _
            & "','" _
            & xxxx _
            & "')")

        ElseIf dgv.Rows(k).Cells(4).Value = "102" Then
            rptcmd.CommandText = ("insert into tab2(f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13) " _
            & " values('" _
            & TextBox1.Text _
            & "','" _
            & TextBox6.Text _
            & "','" _
            & TextBox2.Text _
            & "','" _
            & TextBox3.Text _
            & "','" _
            & dgv.Rows(k).Cells(0).Value _
            & "','" _
            & dgv.Rows(k).Cells(1).Value _
            & "','" _
            & dgv.Rows(k).Cells(2).Value _
            & "','" _
            & TextBox4.Text _
            & "','" _
            & dgv.Rows(k).Cells(3).Value _
            & "','" _
            & TextBox5.Text _
            & "','" _
            & Label2.Text _
            & "','" _
            & username _
            & "','" _
            & xxxx _
            & "')")
        End If
        rptcmd.ExecuteNonQuery()
        rptcon.Close()
    Next k
  End Sub

But when i press the button to perform the above code, it takes a while how can i make it faster?

As the very first thing, do move the connection out of the loop:

billcon.Open()
billcmd.Connection = billcon
For i = 0 To dgv.Rows.Count - 1
    ' ...
Next
billcon.Close()

You can use Multi Thread to speed up the code. Task Class

This also version for vb. Try to use background thread for height weight handle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM