1

目前,我创建了一个允许用户输入 SQL 代码的类,然后该类将结果返回到他们可以进一步使用的数组中。大多数方法使用循环将数据从 OleDbDataReader 对象传输到数组。在处理大量项目时,这可能会非常慢。

当前方法:

Dim SQLdr As OleDbDataReader  'SQL reader
Dim SQLCmd As New OleDbCommand() 'The SQL Command
Dim firstline As Boolean
SQLCmd.Connection = SQLConn 'Sets the Connection to use with the SQL Command
SQLCmd.CommandText = SQLStr 'Sets the SQL String
SQLdr = SQLCmd.ExecuteReader 'Gets Data

然后后来..

While (SQLdr.Read)
  If firstline = True Then
    'fill headers
    Do Until j = SQLdr.FieldCount
      result(j, i) = SQLdr.GetName(j)
      j = j + 1
    Loop
    firstline = False
    j = 0
    i = 1
  End If

  j = 0
  Do Until j = SQLdr.FieldCount
    ReDim Preserve result(result.GetUpperBound(0), result.GetUpperBound(1) + 1)
    If display = True Then
      MsgBox(j & ":" & i & SQLdr(j).ToString)
    End If
    result(j, i) = SQLdr(j).ToString
    j = j + 1
  Loop

  i = i + 1
End While

我想知道是否有更直接的方法将结果输出到数组中。我很抱歉,但我不知道从哪里开始,如果有可能,或者是否有人尝试过前。

4

2 回答 2

3

这真的是 VB.NET 吗?但是,您不应该使用它ReDim Preserve来调整数组的大小。而是使用泛型List及其Add方法。您还应该为您的数据使用自定义类,以提高可读性,使其更易于重用且不易出错。当您不在Object任何地方使用时,它也会更快,因为它不需要装箱/拆箱。

这是一个示例,List(Of User)其中 whereÙser是具有两个属性的自定义类。

Dim users = New List(Of User)
Using con = New OleDb.OleDbConnection(connectionString)
    Using cmd = New OleDb.OleDbCommand("SELECT UserID, UserName FROM dbo.User ORDER BY UserName", con)
        con.Open()
        Using rdr = cmd.ExecuteReader()
            While rdr.Read()
                Dim user = New User()
                user.UserID = rdr.GetInt32(0)
                user.UserName = rdr.GetString(1)
                users.Add(user)
            End While
        End Using
    End Using
End Using 

这里是简单的类:

Class User
    Public Property UserID As Int32
    Public Property UserName As String
End Class

如果你想让你的代码保持动态,你也可以使用 aDataAdapter来填充DataTable/ DataSet。这将简化代码批次,也将更有效率。

Dim table = New DataTable()
Using con = New OleDb.OleDbConnection(connectionString)
    Using da = New OleDb.OleDbDataAdapter("SELECT UserID, UserName FROM dbo.User ORDER BY UserName", con)
        da.Fill(table)
    End Using
End Using
于 2013-04-18T20:19:56.310 回答
1

感谢史蒂夫最初为我指明了正确的方向,也感谢蒂姆。我在这里搜索并找到了解决方案:

http://www.vbforums.com/showthread.php?381224-Filling-a-DataTable-using-a-DataReader

我使用了稍微修改过的方法,因此它包括标题,并且像我之前所做的那样将它作为数组输出。

对于将来的任何人,这里是我完成的代码,它运行 TON 更快地加载结果:

  load_sql(username_, password_, conn_string)

    If SQLConn.State = ConnectionState.Open Then
        Dim myDataTable As New DataTable
        Try
            Using con As New Odbc.OdbcConnection(conn_string)
                ' Dim command As New Odbc.OdbcCommand(SQLConn, con)
                Dim SQLCmd As New OleDbCommand()
                SQLCmd.Connection = SQLConn 'Sets the Connection to use with the SQL Command
                SQLCmd.CommandText = SQLStr 'Sets the SQL String
                Using dr As OleDbDataReader = SQLCmd.ExecuteReader
                    myDataTable.Load(dr)
                End Using
            End Using
        Catch ex As Exception
            myDataTable = Nothing
        End Try


        Dim total_rows As Integer
        Dim total_columns As Integer
        total_rows = myDataTable.Rows.Count
        total_columns = myDataTable.Columns.Count
        Dim result(total_columns, total_rows) As String
        Dim i As Integer = 0
        Dim j As Integer = 0
        Do Until j = total_columns 'add column headers first
            result(j, 0) = myDataTable.Columns(j).Caption
            j = j + 1
        Loop

        Do Until i = total_rows 'load data to array
            RaiseEvent progress(i, total_rows)
            j = 0
            Do Until j = total_columns
                result(j, i + 1) = myDataTable.Rows(i)(j)
                j = j + 1
            Loop

            i = i + 1
        Loop
        RaiseEvent progress(total_rows, total_rows)
        RaiseEvent query_finished(result, queryindex) 'display results

    End If
于 2013-04-19T15:13:33.837 回答