我有一张excel表,我用这样的代码阅读
Public Async Function ReadRecordAsync() As Task(Of SellingList)
Try
Dim Sellings As New SellingList
Await Conn.OpenAsync()
Cmd = New OleDbCommand()
Cmd.Connection = Conn
Cmd.CommandText = "Select * from [Sheet1$]"
Dim Reader = Await Cmd.ExecuteReaderAsync()
While Reader.Read()
If CheckEmptyReader(Reader) Then
MsgBox("First Empty Cell Index = " & Reader.Depth)
Exit While
End If
Dim x As New BuyableObject
If Not Reader.IsDBNull(0) Then
x.Name = Reader.GetString(0)
End If
If Not Reader.IsDBNull(1) Then
x.SoldCount = GetIntegerValue(Reader(1).ToString)
End If
If Not Reader.IsDBNull(2) Then
x.SellingPrice = GetSingleValue(Reader(2).ToString)
End If
If Not Reader.IsDBNull(3) Then
x.OriginalPrice = GetSingleValue(Reader(3).ToString)
End If
x.UpdateCounts()
If x.Name IsNot Nothing Then
Sellings.Add(x)
End If
End While
Reader.Close()
Conn.Close()
Return Sellings
Catch ex As Exception
MsgBox(ex.ToString)
Return New SellingList
End Try
End Function
我想在
获取当前行索引MsgBox("First Empty Cell Index = " & Reader.Depth)
但它不起作用,总是返回0 并检查空读者:
Public Function CheckEmptyReader(reader As DbDataReader) As Boolean
If IsDBNull(reader(0)) AndAlso IsDBNull(reader(1)) AndAlso IsDBNull(reader(2)) AndAlso IsDBNull(reader(3)) Then
Return True
Else
Return False
End If
End Function
那么如何使用阅读器获取当前行索引?
答案 0 :(得分:0)
我通过添加一个简单的计数器找到了一个简单的解决方案
Dim counter As Integer = 1 'start index
While Reader.Read()
If CheckEmptyReader(Reader) Then
MsgBox("First Empty Row Index = " & counter)
Exit While
End If
'some code
counter += 1
End While