使用VB.net和Oracle 10g DB显示记录计数

时间:2011-01-28 15:07:49

标签: sql vb.net oracle visual-studio-2010 oracle10g

我正在尝试在以下查询中获取记录计数:

myCommand = New OleDbCommand("SELECT status, date, theTitle, 
                             theMessage, date2, COUNT(*) over () as countNum 
FROM Blah blah... 

dr = myCommand.ExecuteReader()
MessageBox.Show(dr(5))

但它只是关闭我的程序。如果我对此进行评论,那么它就会很好地填充并且不会关闭。

我该怎么做才能纠正这个问题?

相关问题Record count using Read()

更新:搞定了。我只是错误地将dr(5)放在了Dr dr.Read()

之后。

大卫

1 个答案:

答案 0 :(得分:2)

我发布这篇文章以希望能够走上正确的轨道

Private Shared Sub Main(args As String())
    Console.WriteLine("Demo: reader ")

    ' Connect
    Dim connectStr As String = getConnection()

    Dim SQL As String
    SQL = "select STATUS , date1  , theTitle  , theMessage  , date2  , COUNT( * ) over( ) as countNum   from    (select        TO_CHAR( level, '000' ) STATUS      , sysdate date1      , level || 'aaa' theTitle      , sysdate + 1 DATE2      , level || 'bbb' theMessage      , count( * ) over( ) countRows       from        dual        connect by level < 10    )"

    Dim connection As New OracleConnection(connectStr)
    Dim cmd As New OracleCommand(SQL, connection)
    cmd.CommandType = CommandType.Text

    Try
        connection.Open()
        Dim reader As OracleDataReader = cmd.ExecuteReader()
        While reader.Read()
            Console.WriteLine(reader(0))
            Console.WriteLine(reader(1))
            Console.WriteLine(reader(2))
            Console.WriteLine(reader(3))
            Console.WriteLine(reader(4))
            Console.WriteLine(reader(5))

        End While
    Catch e As Exception
        Console.WriteLine(e.Message)
    End Try

    Console.WriteLine("Done")
End Sub

现在这可行(注意我必须将保留字“date”更改为“date1”

继续尝试(或至少查询)并查看它是否运行。如果它确实运行,那么我会怀疑查询的返回数据有问题

您的错误消息指示数据读取器中没有第5个(或可能是第4个)元素