我有以下代码;
Dim rdr As SqlCeDataReader = cm_sel.ExecuteReader
If rdr.HasRows Then
While rdr.Read
Documents.DeleteDocument(rdr.Item("fID"))
End While
End If
我得到的错误是:
如果是,SQL Server Compact不支持对HasRows属性的调用 底层游标不可滚动。
那么在实际读取DataReader之前我应该如何检查数据是否存在?
修改
整个代码:
Dim con As New SqlCeConnection(ConfigurationManager.ConnectionStrings("MyConnectionString").ToString)
Dim cm_sel As New SqlCeCommand("SELECT fID FROM Files WHERE fCatID=" & catID, con)
Try
con.Open()
Dim rdr As SqlCeDataReader = cm_sel.ExecuteReader
If rdr.HasRows() Then
While rdr.Read
Documents.DeleteDocument(rdr.Item("fID"))
End While
End If
Return "{'result':'ok'}"
Catch ex As Exception
Return "{'result':'error'}"
Finally
con.Close()
con.Dispose()
End Try
答案 0 :(得分:5)
您可以直接调用reader.Read
,因为如果没有行,则返回false。只需删除if
周围的while
声明。
更新代码:
Dim rdr As SqlCeDataReader = cm_sel.ExecuteReader
While rdr.Read
Documents.DeleteDocument(rdr.Item("fID"))
End While
答案 1 :(得分:0)
这适用于上述问题。
Dim hasrows As Boolean
hasrows = False
While (rdr.Read())
hasrows = True
End While
If (hasrows) Then
--do something
else
--do something else
end if
If you assign hasrows function to any Boolean and try to use it, the reader can jump to second record. The above solution will avoid that scenario as well.