1

我不知道我的程序发生了什么,我花了(4)四分钟来加载我的代码结果......有人可以告诉我为什么吗?有人能告诉我如何解决这个加载问题吗?

这是我的代码:

Imports System.Data.SqlClient
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim str As String = "Data Source=######;Initial Catalog=###;Persist Security Info=True;User ID=#####;Password=#####"
        Dim con As New SqlConnection(str)
        Dim cmd As String = "Select ControlNo,EmpNo,CheckOutDate,CheckOutTime,TaxiNo,PlateNo,Model,Make from dbo.ChkInOut"
        Dim adpt As New SqlDataAdapter(com, con)
        Dim myDataSet As New DataSet()
        adpt.Fill(myDataSet, "dbo.ChkInOut")
        Dim myDataTable As DataTable = myDataSet.Tables(0)
        Dim tempRow As DataRow
        For Each tempRow In myDataTable.Rows
            'ListBox1.Items.Add((tempRow("ControlNo") & " (" & tempRow("EmpNo") & ")" & " (" & tempRow("CheckOutDate") & ")" & " (" & tempRow("CheckOutTime") & ")" & " (" & tempRow("TaxiNo") & ")" & " (" & tempRow("PlateNo") & ")" & " (" & tempRow("Model") & ")" & " (" & tempRow("Make") & ")"))
            'ListBox1.Items.Add((tempRow("ControlNo") & " (" & tempRow("EmpNo") & ")"))
            ListBox1.Items.Add(tempRow("ControlNo") & "            " & tempRow("EmpNo") & "            " & tempRow("CheckOutDate") & "            " & tempRow("CheckOutTime") & "            " & tempRow("TaxiNo") & "            " & tempRow("PlateNo") & "            " & tempRow("Model") & "            " & tempRow("Make") & "            ")
        Next
    End Sub

End Class
4

2 回答 2

4

首先,我必须同意上面的问题,返回了多少数据。除此之外,我是否可以建议不要循环遍历 DataTable 并填充 ListBox,而是绑定数据:

Dim myDataSet As New DataSet()
adpt.Fill(myDataSet, "dbo.ChkInOut")

ListBox1.DataTextField = "yourtext"
ListBox1.DataValueField = "yourvalue"
ListBox1.Datasource = myDataSet
ListBox1.DataBind()

这可能只会提高性能。

于 2013-01-30T06:11:53.997 回答
4

道歉...以为是一个网络应用程序。

试试这个:

ListBox1.DataSource = myDataTable 
ListBox1.DisplayMember = "ColumnName"
于 2013-01-30T06:43:58.550 回答