我的vb 2005项目中有以下功能
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
我从包含gridview的其他表单(dgView有50000条记录)中调用它
For i as integer=0 to dgView.RowCount - 1
dgView.Item(7, i).Value = Getrawmatstockwdate(dgView.Item(0, i).Value, dgView.Item(5, i).Value, dtTo.DateTime.Date.ToString("yyyy-MM-dd"))
Next
它可能需要多达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中格式化它。