簡體   English   中英

我怎樣才能更快地執行此代碼?

[英]how can i execute this code faster?

在我的“帳單”表單中,我有一個按鈕可以將datagridview數據插入兩個訪問數據庫中。 我使用此代碼插入到第一個數據庫:

    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

我使用此代碼插入到第二個數據庫:

    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

但是,當我按下按鈕執行上述代碼時,我需要花些時間才能使其更快?

首先,請將連接移出循環:

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

您可以使用多線程來加速代碼。 任務類別

這也是vb的版本。 嘗試將背景螺紋用於身高體重手柄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM