Excel vba从设置为readonly的oracle数据库中读取数据

时间:2017-06-03 17:57:51

标签: database excel-vba vba excel

我正在尝试使用excel中的adodb连接对象从oracle读取数据。但是我收到错误" oracle -160000数据库设置为只读访问"。请告诉我如何解决此问题。

1 个答案:

答案 0 :(得分:0)

查看记录集open方法的LockType参数并使用adLockReadOnly。我不熟悉Oracle。您可能还需要将CursorType参数添加到adOpenForwardOnly。

示例:

Dim lobjADOConnection As ADODB.Connection
Dim lobjADOData As ADODB.Recordset
Dim lstrSQL As String

Set lobjADOConnection = New ADODB.Connection
lobjADOConnection.Open ...

Set lobjADOData = New ADODB.Recordset

lstrSQL = "<your query here>"

lobjADOData.Open lstrSQL, lobjADOConnection, adOpenForwardOnly, adLockReadOnly '<===here
If Not lobjADOData.BOF Then
    lobjADOData.MoveFirst
    Do While Not lobjADOData.EOF
        DoEvents
        .
        .
        .

或者您只想将数据转储到工作表中

Dim lobjTargetSheet As Worksheet

Set lobjTargetSheet = ThisWorkbook.Sheets("<SomeSheetName>")
With lobjTargetSheet
    .Select
    .Range("1:" & .UsedRange.Rows.count + 1).Delete xlUp
    .Range("A1").Select
    .Range("A1").CopyFromRecordset lobjADOData
End With