I’m attempting to make a generic query executer function. I want to be able to send it a query string and have it return a two-dimensional array containing the results. Below is my code for how to do it with a (9,x) array. How can I do it with an (y,x) sized array? Also, I feel like there must be a more efficient way to do this…
Dim right As Integer = 0
dbConn = New SqlConnection("hidden for security purposes")
MyCommand = New SqlCommand(queryString, dbConn)
dbConn.Open()
Dim resultArray(9, 0) As String
MyDataReader = MyCommand.ExecuteReader()
While (MyDataReader.Read())
For i = 0 To 9
If IsDBNull(MyDataReader(i)) Then
'resultArray(i, UBound(resultArray, 2)) = ""
Else
resultArray(i, UBound(resultArray, 2)) = MyDataReader(i)
End If
Next
ReDim Preserve resultArray(9, UBound(resultArray, 2) + 1)
End While
ReDim Preserve resultArray(9, UBound(resultArray, 2) - 1)
MyDataReader.Close()
dbConn.Close()
Return resultArray
Try something like this:
For a query with no parameters, call it like this:
Or if you do have a parameter: