2

是否可以在读取 MySQL 响应之前接收它的大小?能够有一个进度条,提供有关接收过程的状态。

Public Function QUERY(ByVal queryString As String, ByVal connection As MySqlConnection)
Try
    Dim newQuery As String() = Split(queryString, ":")
    For Each Query In newQuery
        Dim cmd As New MySqlCommand(Query, connection)
        Dim reader As MySqlDataReader
        reader = cmd.ExecuteReader()
        While reader.Read()
        End While
        reader.Close()
    Next
Catch ex As Exception
    console("Error: " & ex.Message)
    Return ex.Message
End Try
Return ""
End Function
4

1 回答 1

2

不,使用 DataReader 在到达阅读器末尾之前,您无法知道查询返回的记录数。但是,您可以在查询中使用多个语句,其中第一个语句是您的数据计数。

这是一个可能的例子:

using con = new MySqlConnection("your_connection_string")
    con.Open()
    Dim cmd As MySqlCommand = new SqlCommand("SELECT COUNT(*) FROM MyTable;" & _
                                             "SELECT * FROM MyTable", con)
    Dim dr as MySqlDataReader = cmd.ExecuteReader()
    if dr.HasRows Then        
        dr.Read()
        Dim count = Convert.ToInt32(dr(0))
        Console.WriteLine("Reading:" & count.ToString() & " records")
        dr.NextResult()
        while dr.Read()
            Console.WriteLine("Record:" + dr(dr.GetOrdinal("TableID")).ToString())
        End While
    End if
End Using
于 2013-08-08T20:43:22.550 回答