使用 ODBC 从 Excel VBA 连接到 Oracle 10g

发布于 2024-08-10 21:23:56 字数 657 浏览 2 评论 0原文

以下代码有效。连接打开正常,但当表中有数据时,recordset.recordCount 始终返回 -1。如果我尝试调用记录集上的任何方法/属性,它会使 Excel 崩溃。有什么想法吗? 谢谢

Sub GetData()
    Dim conn As New ADODB.connection    
    Dim connString
    connString = "DSN=name;Uid=user;Pwd=pass"
    Dim rsRecords As New ADODB.recordSet

    conn.Open connString
    rsRecords.CursorLocation = adUseServer
    rsRecords.Open "select * from xxx", conn, adOpenForwardOnly, adLockReadOnly

    If conn.State = adStateOpen Then
      MsgBox rsRecords.RecordCount
    Else
       MsgBox "no connection"
    End If

    rsRecords.Close
    Set rsRecords = Nothing
    conn.Close
    Set conn = Nothing
End Sub

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes Excel. Any ideas??
Thanks

Sub GetData()
    Dim conn As New ADODB.connection    
    Dim connString
    connString = "DSN=name;Uid=user;Pwd=pass"
    Dim rsRecords As New ADODB.recordSet

    conn.Open connString
    rsRecords.CursorLocation = adUseServer
    rsRecords.Open "select * from xxx", conn, adOpenForwardOnly, adLockReadOnly

    If conn.State = adStateOpen Then
      MsgBox rsRecords.RecordCount
    Else
       MsgBox "no connection"
    End If

    rsRecords.Close
    Set rsRecords = Nothing
    conn.Close
    Set conn = Nothing
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城°AllureLove 2024-08-17 21:23:56

如果您希望 Recordcount 正常工作,尤其是在不获取记录的情况下,则需要使用 adUseClient 而不是 adUseServer

如果这是一项大型操作,并且您会经常执行此操作,您实际上应该选择 count() 并让数据库为您进行计数。使用 adUseClient,无论您是否愿意,ADO 都会默默地将所有记录提取到应用程序的内存中。

If you want Recordcount to work, especially without fetching the records, you need to use adUseClient not adUseServer

If this is a large operation and you'll be doing it frequently, though, you should really select the count() instead and make the database do the counting for you. With adUseClient ADO will silently fetch all the records into your application's memory whether you like it or not.

指尖上的星空 2024-08-17 21:23:56

Excel 似乎崩溃了,因为它无法处理某些 Oracle 数据类型。
select * 返回类型为 TIMESTAMP(3) WITH TIME ZONE 的列。
当我尝试从记录集中读取数据时,这会导致 Excel 崩溃。

It seems Excel was crashing because it cannot handle some Oracle datatypes.
The select * was returning a column of type TIMESTAMP(3) WITH TIME ZONE.
This would cause Excel to crash as soon as I tried to read from the recordset.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文