gpt4 book ai didi

excel - 找不到 OleDB 数据提供程序 VBA/Excel

转载 作者:行者123 更新时间:2023-12-02 08:18:27 31 4
gpt4 key购买 nike

我几乎不熟悉 VBA(在学校学过一些类(class),仅此而已)。现在我需要从 Excel 文件连接到 Oracle 数据库(在远程服务器上运行)。我环顾四周并找到了一些例子。因此,到目前为止我编写了以下代码:

    Sub Try()
Dim cn As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim cmd As ADODB.Command
Dim chunk() As Byte
Dim fd As Integer
Dim flen As Long
Dim Main As ADODB.Parameter
Dim object As ADODB.Parameter

Stil = vbYesNo + vbCritical + vbDefaultButton1
Titel = "db connection test"
' Meldung anzeigen.
Antwort = MsgBox("trying to connect to db", Stil, Titel, Hilfe, Ktxt)

' Connect to the database using ODBC [msdaora][ORAOLEDB.Oracle]Provider=ORAOLEDB.Oracle;
With cn
.ConnectionString = "Provider=ORAOLEDB.Oracle;Password=pass;User ID=usr;Data Source=host:port:sid"
.Open
.CursorLocation = adUseClient
End With

ret = cn.Execute("create table newtesttable (main integer, object oid)")

' Here is an example if you want to issue a direct
' command to the database
'
'Set cmd = New ADODB.Command
'With cmd
' .CommandText = "delete from MYTABLE"
' .ActiveConnection = cn
' .Execute
'End With
'Set cmd = Nothing

'
' Here is an example of how insert directly into the
' database without using
' a recordset and the AddNew method
'
Set cmd = New ADODB.Command
cmd.ActiveConnection = cn
' cmd.CommandText = "insert into newtesttable(main,object) values(?,?)"
cmd.CommandText = "select * from test"
cmd.CommandType = adCmdText

' The main parameter
' Set main = cmd.CreateParameter("main", adInteger, adParamInput)
'main.Value = 100 '' a random integer value ''
'cmd.Parameters.Append main

' Open the file for reading
'fd = FreeFile
'Open "myBlobFile.txt" For Binary Access Read As fd
'flen = LOF(fd)
'If flen = 0 Then
' Close
' MsgBox "Error while opening the file"
' End
'End If

' The object parameter
'
' The fourth parameter indicates the memory to allocate
' to store the object
' Set object = cmd.CreateParameter("object", _
' adLongVarBinary, _
' adParamInput, _
flen + 100)
' ReDim chunk(1 To flen)
' Get fd, , chunk()

' Insert the object into the parameter object
' object.AppendChunk chunk()
' cmd.Parameters.Append object

' Now execute the command
Set rs = cmd.Execute

' Mldg = "test"
Stil = vbYesNo + vbCritical + vbDefaultButton1
Titel = "asdasdasd"
' Meldung anzeigen.
Antwort = MsgBox(rs, Stil, Titel, Hilfe, Ktxt)
' ... and close all
cn.Close
Close

End Sub

我相信这段代码有很多问题,但目前在尝试执行.Open时失败,说“找不到Provider。它可能没有正确安装”。之后我发现我需要下载并安装ORAOLEDB.dll。我通过安装 ORAOledb11.dll 来做到这一点(我尝试过 32 位和 64 位,你我的机器是 64 位)。我通过执行 regsvr32 OraOLEDB11.dll 来安装它。

不幸的是问题仍然存在。那么,可以采取哪些步骤来解决这个问题呢?我能否以某种方式确保 Oraoledb 已正确安装在我的计算机上?

任何提示将不胜感激。

最佳答案

32位操作系统

我通过从Oracle官方网站Oracle10g Provider for OLE DB Version 10.1.0.4.0下载oracle OLEDB provider,成功在Windows XP虚拟机上运行。当前工作link适用于旧操作系统的 OLEDB(32 位)

但请注意,它会将 JDK 和 JRE 替换为较低版本(可以通过使用配置 xml - products.xml 来防止它 - 我没有足够的心理健康药水,所以我而是完全安装)。之后您需要删除环境变量中的引用,因为它可能会影响其他程序。安装后,我用regsvc32注册了OraOLEDBxx.dll

我使用 Excel 2003 连接到 oracle db 11G。:)

连接字符串

我必须启用扩展(ActiveX 数据对象和记录库)。我返回连接的函数是:

Public Function connectToDb(provider As String, host As String, sid As String, user As String, pwd As String, db As String) As ADODB.Connection
Dim conn As ADODB.Connection
Dim dbConnectStr As String

Set conn = New ADODB.Connection
If provider = "Oracle" Then
dbConnectStr = "Provider=OraOLEDB.Oracle;Data Source=" & host & ":1521/" & sid & ";User Id=" & user & ";Password=" & pwd & ";"
Else

End If
conn.ConnectionString = dbConnectStr
conn.Open
Set connectToDb = conn
End Function

64 位操作系统,但 32 位 Office

当我们的虚拟机迁移到带有 Excel 2010 的 64 位 Windows 7 时。确保您将下载 ODAC - Oracle 数据访问组件 - 适合您的 Excel 安装的正确版本,因为我安装了 32 位 Excel,并认为它是 64 位(因为 Windows 是 64 位)所以我正在尝试让它与 64 位 ODAC 版本一起工作。后来我下载了32位版本,它的工作原理和以前一样。对于安装,只需按照下载的存档文件夹中包含的安装说明进行操作即可。

当前工作links Oracle 网站上的 ODAC

关于excel - 找不到 OleDB 数据提供程序 VBA/Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14836274/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com