-1

我很困惑为什么 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的新手,对它真的有一点了解,请帮助我,并提前谢谢你..

4

1 回答 1

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都会引发异常。

于 2016-12-01T05:54:19.593 回答