加载下拉列表的速度更快

时间:2018-04-02 18:33:48

标签: asp.net sql-server vb.net webforms

所以我在asp.net web表单站点中加载下拉列表时遇到问题。我从SQL Server数据库中提取记录。我将结果绑定到下拉列表。

我的问题源于这样一个事实,即我正在检索21500多行,这导致了一个长时间的延迟,浏览器会抛出一条消息,询问我是否要停止执行长脚本。如果我等了足够长的时间,大约2分钟就会回来,但是在很长一段时间内向下滚动列表仍然会很慢。

以下是SQL调用的VB代码:

    Private Function GetCorInfo(field As String, tblname As String, Optional whereClause As String = "") As DataTable
    Dim sqlCmdTxt As String = "Select " & field & " From " & tblname
    Using conn As New SqlConnection(corConnection)
        Try
            conn.Open()
        Catch ex As Exception
            Master.message = "Unable to open SQL DB connection\nError: SQL101\nPlease contact the Help Desk for support.\n" & HttpUtility.JavaScriptStringEncode(ex.Message)
            jsa.alertmessage(passedPage, Master.message)
        End Try
        Using sqlCmd As New SqlCommand
            'Check for where clause
            If whereClause <> "" Then
                sqlCmdTxt = sqlCmdTxt & whereClause
                If whereClause.Substring(7, 6) = "cornum" Then
                    sqlCmd.Parameters.AddWithValue("@cornum", ddl2.SelectedItem.Text)
                End If
            End If
            If field = "cornum" Then
                sqlCmdTxt = sqlCmdTxt & " Order By " & field & " Desc"
            End If
            sqlCmd.CommandText = sqlCmdTxt
            sqlCmd.Connection = conn
            Using sqlDT As New DataTable()
                Using sqlDA As New SqlDataAdapter(sqlCmd)
                    Try
                        sqlDA.Fill(sqlDT)
                        Return sqlDT
                    Catch ex As Exception
                        conn.Close()
                        Master.message = "Unable to load list.\nError: SQL104\n" & HttpUtility.JavaScriptStringEncode(ex.Message)
                        jsa.alertmessage(passedPage, Master.message)
                        Return Nothing
                    End Try
                End Using
            End Using
        End Using
    End Using
End Function

然后,当返回数据表时,我使用以下代码将其绑定到下拉列表:

                If Not IsNothing(dt) Then
                'Set ddl
                With ddl
                    'Turn on ddl
                    .Visible = True
                    'Set Data Source
                    .DataSource = dt
                    'Set Text Field
                    .DataTextField = field1
                    'Set Value Field
                    .DataValueField = field1
                    'Set variable to field value
                    'Bind Data
                    .DataBind()
                    'Assign Variable
                    field = ddl.SelectedItem.Text
                    'Check for ddl match
                    If whereClause <> "" AndAlso ddl1.SelectedIndex = 3 AndAlso ddl.ID = "ddl3" Then
                        .Items.FindByValue(field).Selected = True
                    ElseIf whereClause <> "" AndAlso ddl1.SelectedIndex = 3 AndAlso ddl.ID = "ddl4" Then
                        .Items.Insert(0, New ListItem("Select", "0"))
                    Else
                        'Insert first choice
                        .Items.Insert(0, New ListItem("Select", "0"))
                    End If
                    'Set to index 0
                    .SelectedIndex = 0
                End With
                Return 1
            Else
                Return -1
            End If

这两组代码都在函数中运行。

我怎样才能加快速度?我已经看过使用Session和View State但是如果我这样做的话,返回的记录数会导致更大的减速。

有没有人有任何想法?

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

所以这就是我最终做的。

放置一个文本框供用户输入新客户名称的前几个字符。然后,我构建一个SQL查询,只返回与输入的字符匹配的记录。

快得多,只有最多35条记录......