2

我正在尝试制作通用查询执行器功能。我希望能够向它发送一个查询字符串并让它返回一个包含结果的二维数组。下面是我如何使用 (9,x) 数组执行此操作的代码。如何使用 (y,x) 大小的数组来做到这一点?另外,我觉得必须有一种更有效的方法来做到这一点......

   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
4

3 回答 3

2

尝试这样的事情:

Public Function GetData(ByVal queryString As String, ByVal addParameters As Action(Of SqlParameterCollection) As DataTable
    Dim result As New DataTable()
    Using cn As New SqlConnection(" connection string "), _
          cmd As New SqlCommand(queryString, cn)

       addParameters(cmd.Parameters)

       cn.Open()
       Using rdr As SqlCommand.ExecuteReader()
          result.Load(rdr)
       End Using
   End Using
   Return result
End Function

对于没有参数的查询,这样调用它:

Dim result = GetData("Select * from Table", Sub(p) Exit Sub)

或者,如果您确实有一个参数:

Dim result = GetData("Select * from Table Where ID= @ID", _
    Sub(p)
        p.Add("@ID", SqlDbType.Int).Value = 12345
    End Sub)
于 2012-06-11T15:03:53.800 回答
0

使用 List 并交换 x 和 y 会更容易和更有效

Dim result = new List(Of String())

列表会自动增长。

While (MyDataReader.Read())
    Dim row = new String(MyDataReader.FieldCount - 1)
    For i = 0 To MyDataReader.FieldCount - 1
        row(i) = MyDataReader(i).ToString()
    Next
    result.Add(row)
End While
于 2012-06-11T15:08:35.803 回答
0

我提醒说这是一个坏主意,但如果你必须这样做,我建议返回一个数据集而不是一个数组:

Private Function getDataSet(ByVal queryString As String) As DataSet
    Dim data As New DataSet()
    Using dbConn As New SqlConnection("hidden for security purposes")
        dbConn.Open()
        Using MyCommand As New SqlCommand(queryString, dbConn)
            Using adapter As New SqlDataAdapter(MyCommand)
                adapter.Fill(data)
            End Using
        End Using
    End Using
    Return data
End Function

如果您不想要数据集,则可以只返回数据集中的第一个表(本质上是数据的二维数组,但具有更多功能和元数据):

Private Function getDataTable(ByVal queryString As String) As DataTable
    Dim data As New DataSet()
    Using dbConn As New SqlConnection("hidden for security purposes")
        dbConn.Open()
        Using MyCommand As New SqlCommand(queryString, dbConn)
            Using adapter As New SqlDataAdapter(MyCommand)
                adapter.Fill(data)
            End Using
        End Using
    End Using
    Return data.Tables(0)
End Function

或者,如果您必须有一个二维字符串数组,您仍然可以通过在内部填充数据集然后将其转换为数组来更轻松:

Private Function getDataArray(ByVal queryString As String) As String()()
    Dim rows As List(Of String()) = New List(Of String())
    Using dbConn As New SqlConnection("hidden for security purposes")
        dbConn.Open()
        Using MyCommand As New SqlCommand(queryString, dbConn)
            Using adapter As New SqlDataAdapter(MyCommand)
                Dim data As New DataSet()
                adapter.Fill(data)
                For Each row As DataRow In data.Tables(0).Rows
                    Dim items(row.ItemArray.Length) As String
                    For i As Integer = 0 To row.ItemArray.Length - 1
                        items(i) = row.ItemArray(i).ToString()
                    Next
                    rows.Add(items)
                Next
            End Using
        End Using
    End Using
    Return rows.ToArray()
End Function
于 2012-06-11T15:02:57.317 回答