Dropdownlist需要很长时间才能加载VB 6.0

时间:2015-08-23 10:55:33

标签: vb6

我正在使用vb6和sql server.in,在表单上加载我填充4个组合框。但是需要10到12分钟来加载表单。 我的代码如下: 任何人都可以帮我快速加载表单吗?

Public Sub fillCombo(Id As String, Name As String, Table As String, obj As Object, Optional cond As String)
    Dim rsF  As New ADODB.Recordset
    With rsF
    If .State = adStateOpen Then .Close
    If cond = "" Then
    .Open "Select " & Id & "," & Name & " From " & Table & " Order by " & Name, Cn, adOpenKeyset, adLockOptimistic
    Else
    .Open "Select " & Id & "," & Name & " From " & Table & " Where " & cond & " Order by " & Name, Cn, adOpenKeyset, adLockOptimistic
    End If
    obj.Clear
    'obj.AddItem ""
    While Not .EOF
    obj.AddItem .Fields(1)
    obj.ItemData(obj.NewIndex) = .Fields(0)
    .MoveNext
    Wend
    .Close
    End With
    End Sub

函数调用如下:     fillCombo" JobId"," JobName"," Jobs",cboJob

1 个答案:

答案 0 :(得分:0)

问题(我相信)是您正在使用服务器端记录集。当你这样做时,你正在为你的循环的每次迭代进行一次往返服务器,正如你所发现的那样,冰冷的慢。

解决方案是创建客户端记录集。这样就可以一次性将数据从服务器发送到客户端。请记住,客户端记录集始终是静态的;如果将CursorType设置为adOpenKeyset,将CursorLocation设置为adUseClient,后者将覆盖前者,而CursorType仍将是adOpenStatic。

这是你的代码的一个mod,有各种改进(特别是你的残余缩进得到纠正;你可能会认为那些在编写代码时必须使用你的代码的穷人会更好一些):

Public Sub fillCombo(Id As String, Name As String, Table As String, obj As Object, Optional cond As String)

    Dim sql As String
    Dim rsF As ADODB.Recordset 'Don't use "As New" in VB6, it's slow

    'sql variable with ternary conditional (IIf) is cleaner way to do what you want
    sql = "Select " & Id & "," & Name & " From " & Table & IIf(cond = "", "", " Where " & cond) & " Order by " & Name
    Set rsF = New ADODB.Recordset
    With rsF
        .CursorLocation = adUseClient 'Client-side, static cursor

        'If .State = adStateOpen Then .Close --Get rid of this: if you just created rsF, it can't be open, so this is a waste of processor cycles

        .Open sql, Cn 'Much prettier, yes?  :)
        obj.Clear 'All this is fine
        Do Until .EOF
            obj.AddItem .Fields(1)
            obj.ItemData(obj.NewIndex) = .Fields(0)
            .MoveNext
        Loop
        .Close
    End With

End Sub