目前,我创建了一个允许用户输入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
我想知道是否有更直接的方式将结果输出到数组中...我很抱歉但我不知道从哪里开始,如果有可能,或者如果有人以前尝试过这个。
答案 0 :(得分:3)
这真的是VB.NET吗?但是,您不应使用ReDim Preserve
来调整阵列大小。而是使用通用List
及其Add
方法。您还应该为数据使用自定义类,这样可以提高可读性,使其更具可重用性,并且不易出错。如果不在任何地方使用Object
,它也会更快,因为它不需要打包/取消装箱。
以下是List(Of User)
的示例,其中Ù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
如果您想让代码保持动态,您还可以使用DataAdapter
填充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
答案 1 :(得分:0)
感谢史蒂夫最初指出我正确的方向,也谢谢蒂姆。我在这里搜索并找到了解决方案:
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