我很困惑为什么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的新手并且真的对它有一点了解,请帮助我,并提前感谢你..
答案 0 :(得分: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
之前尝试访问任何数据都会引发异常。