【问题标题】:MysqlDataReader.Read stuck on the last record and doesnt EOFMysqlDataReader.Read 停留在最后一条记录上并且没有 EOF
【发布时间】:2016-12-01 05:04:26
【问题描述】:

我很困惑为什么 mySqlDataReader.Read 停留在最后一条记录上而不是 EOF ..

这是我的 executeSql 私有函数:

Private Function executeSQL(ByVal str As String, ByVal connString As String, ByVal returnRecordSet As Boolean) As Object
    Dim cmd As Object
    Dim objConn As Object
    Try
        If dbType = 2 Then
            cmd = New MySqlCommand
            objConn = New MySqlConnection(connString)
        Else
            cmd = New OleDbCommand
            objConn = New OleDbConnection(connString)
        End If
        'If objConn.State = ConnectionState.Open Then objConn.Close()
        objConn.Open()
        cmd.Connection = objConn
        cmd.CommandType = CommandType.Text
        cmd.CommandText = str
        If returnRecordSet Then
            executeSQL = cmd.ExecuteReader()
            executeSQL.Read()
        Else
            cmd.ExecuteNonQuery()
            executeSQL = Nothing
        End If
    Catch ex As Exception
        MsgBox(Err.Description & " @ExecuteSQL", MsgBoxStyle.Critical, "ExecuteSQL")
    End Try
End Function

这是我的 sub 在发生错误的地方调用它:

Using admsDB As MySqlConnection = New MySqlConnection("server=" & rs("server") & ";uid=" & rs("user") & ";password=" & rs("pwd") & ";port=" & rs("port") & ";database=adms_db;")
                    admsDB.Open()
                    connDef.Close()
                    rs.Close()
                    'get record on admsdb
                    Dim logDate As DateTime
                    Dim str As String
                    str = "select userid, checktime from adms_db.checkinout in_out where userid not in (select userid " &
                        "from adms_db.checkinout in_out join (select str_to_date(datetime,'%d/%m/%Y %H:%i:%s') tgl, fid from zsoft_bkd_padang.ta_log) ta " &
                        "on ta.fid=userid and tgl=checktime)"
                    Dim rsAdms As MySqlDataReader = executeSQL(str, admsDB.ConnectionString, True)
                    Dim i As Integer
                    'This is where the error is, datareader stuck on the last record and doesnt EOF
                    While rsAdms.HasRows
                        'i = i + 1 
                        logDate = rsAdms(1)
                        'save to ta_log
                        str = "insert into ta_log (fid, Tanggal_Log, jam_Log, Datetime) values ('" & rsAdms(0) & "','" & Format(logDate.Date, "dd/MM/yyyy") & "', '" & logDate.ToString("hh:mm:ss") & "', '" & logDate & "')"
                        executeSQL(str, oConn.ConnectionString, False)
                        rsAdms.Read()
                    End While

                    'del record on admsdb
                    str = "truncate table checkinout"
                    executeSQL(str, admsDB.ConnectionString, False)
                End Using

我是vbnet的新手,对vbnet有点了解,请帮助我,提前谢谢你..

【问题讨论】:

  • 您是否收到实际的错误消息?
  • 没有实际的错误信息,只是循环没有结束并停留在最后一条记录,

标签: vb.net datareader oledbdatareader mysqldatareader


【解决方案1】:

问题是您使用HasRows 属性作为循环终止表达式。该属性的值永远不会改变。阅读器要么有行,要么没有。它不是检查是否还有行要读取,因此读取没有效果。

您应该使用Read 方法作为您的标志。数据读取器在没有加载行的情况下开始。每次调用Read,它都会加载下一行并返回True,或者,如果没有更多行要读取,则返回False

如果你想在结果集为空时做一些特殊的事情,你通常只使用HasRows,例如

If myDataReader.HasRows Then
    '...
Else
    MessageBox.Show("No matches found")
End If

如果您不想将空结果集视为特殊情况,则只需调用Read

While myDataReader.Read()
    Dim firstFieldValue = myDataReader(0)
    '...
End While

请注意,在调用 Read 之前尝试访问任何数据将引发异常。

【讨论】:

    猜你喜欢
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 2019-08-31
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    相关资源
    最近更新 更多