MysqlDataReader.Read 停留在最后一条记录上,没有 EOF
MysqlDataReader.Read stuck on the last record and doesnt EOF
我很困惑为什么 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
这是我在发生错误的地方调用它的子程序:
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 的新手,对它了解不多,请帮助我,在此先感谢您..
问题是您使用 HasRows
属性 作为循环终止表达式。 属性 的值永远不会改变。 reader 要么有行,要么没有。这不是检查它是否还有行要读取,因此读取无效。
你应该使用 Read
方法作为你的标志。数据 reader 开始时未加载任何行。每次调用 Read
时,它都会加载下一行和 return True
或者,如果没有更多的行要读取,它会 returns 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
之前尝试访问任何数据将引发异常。
我很困惑为什么 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
这是我在发生错误的地方调用它的子程序:
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 的新手,对它了解不多,请帮助我,在此先感谢您..
问题是您使用 HasRows
属性 作为循环终止表达式。 属性 的值永远不会改变。 reader 要么有行,要么没有。这不是检查它是否还有行要读取,因此读取无效。
你应该使用 Read
方法作为你的标志。数据 reader 开始时未加载任何行。每次调用 Read
时,它都会加载下一行和 return True
或者,如果没有更多的行要读取,它会 returns 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
之前尝试访问任何数据将引发异常。