Vb.net 调用函数很慢

Vb.net 调用函数很慢,vb.net,Vb.net,在我的VB2005项目中,我有以下功能 Public Function Getrawmatstockwdate(ByVal Partno As String, ByVal Loc As String, ByVal Tgl As Date) As Double Using con = Openconnection() Using cmd As New MySqlCommand() Dim Stockbal As Decimal = 0.0

在我的VB2005项目中,我有以下功能

Public Function Getrawmatstockwdate(ByVal Partno As String, ByVal Loc As String, ByVal Tgl As Date) As Double 
    Using con = Openconnection()
        Using cmd As New MySqlCommand()
            Dim Stockbal As Decimal = 0.0
            cmd.CommandText = "select round(ifnull(sum(stk.qty),0)," & Decimalpoint & ") from (select op_qty as qty from location where matcode='" & _
            Partno & "' and location='" & Loc & "' union all select in_qty as qty from min_in_body inner join min_in_head on min_in_body.doc_no=min_in_head.doc_no " & _
            "and min_in_body.entry_no=min_in_head.entry_no where matcode='" & Partno & "' and location='" & Loc & "' and in_date<='" & Tgl.ToString("yyyy-MM-dd") & _
            "' union all select (-1*out_qty) as qty from min_out_body inner join min_out_head on min_out_body.doc_no=min_out_head.doc_no where matcode='" & Partno & _
            "' and location='" & Loc & "' and out_date<='" & Tgl.ToString("yyyy-MM-dd") & "') stk
            cmd.CommandType = CommandType.Text
            cmd.Connection = con
            Stockbal = cmd.ExecuteScalar()
            Return Stockbal
        End Using
    End Using
End Function
它可能需要30分钟以上,但如果将函数直接放入循环,如下面的代码所示,只需要5分钟,有人愿意解释为什么吗

For i as integer = 0 to dgView.RowCount - 1 
        Dim Partno As String = dgView.Item(0, i).Value
        Dim Loc As String = dgView.Item(5, i).Value
        'get bal qty
        Dim Stockbal As Decimal = 0.0
        cmd = New MySqlCommand
        cmd.CommandText = "select round(ifnull(sum(stk.qty),0)," & Decimalpoint & ") from (select op_qty as qty from location where matcode='" & _
        Partno & "' and location='" & Loc & "' union all select in_qty as qty from min_in_body inner join min_in_head on " & _
        "min_in_body.doc_no=min_in_head.doc_no " & "and min_in_body.entry_no=min_in_head.entry_no where matcode='" & Partno & _
        "' and location='" & Loc & "' and in_date<='" & dtTo.DateTime.Date.ToString("yyyy-MM-dd") & "' union all select (-1*out_qty) as qty " & _
        "from min_out_body inner join min_out_head on min_out_body.doc_no=min_out_head.doc_no where matcode='" & Partno & _
        "' and location='" & Loc & "' and out_date<='" & dtTo.DateTime.Date.ToString("yyyy-MM-dd") & "') stk"
        cmd.CommandType = CommandType.Text
        cmd.Connection = con
        Stockbal = cmd.ExecuteScalar()

        dgView.Item(7, i).Value = Stockbal            
    Next

希望它是可读的。不知道如何在stackoverflow中设置格式。

为什么要将50000行加载到gridview中?不要,因为你没有每次都打开到数据库的连接。您可以将连接作为参数传递。您还应该使用参数化查询什么是参数化查询,感谢前面的解释。是否类似于cmd.parameter.addvalue?
For i as integer = 0 to dgView.RowCount - 1 
        Dim Partno As String = dgView.Item(0, i).Value
        Dim Loc As String = dgView.Item(5, i).Value
        'get bal qty
        Dim Stockbal As Decimal = 0.0
        cmd = New MySqlCommand
        cmd.CommandText = "select round(ifnull(sum(stk.qty),0)," & Decimalpoint & ") from (select op_qty as qty from location where matcode='" & _
        Partno & "' and location='" & Loc & "' union all select in_qty as qty from min_in_body inner join min_in_head on " & _
        "min_in_body.doc_no=min_in_head.doc_no " & "and min_in_body.entry_no=min_in_head.entry_no where matcode='" & Partno & _
        "' and location='" & Loc & "' and in_date<='" & dtTo.DateTime.Date.ToString("yyyy-MM-dd") & "' union all select (-1*out_qty) as qty " & _
        "from min_out_body inner join min_out_head on min_out_body.doc_no=min_out_head.doc_no where matcode='" & Partno & _
        "' and location='" & Loc & "' and out_date<='" & dtTo.DateTime.Date.ToString("yyyy-MM-dd") & "') stk"
        cmd.CommandType = CommandType.Text
        cmd.Connection = con
        Stockbal = cmd.ExecuteScalar()

        dgView.Item(7, i).Value = Stockbal            
    Next