OleDB数据提供者找不到VBA / Excel

我几乎不熟悉VBA(在学校有一些课程,就是这样)。 现在我需要从Excel文件连接到Oracle数据库(在远程服务器上运行)。 我四处窥视,并find一些例子。 所以,到目前为止我已经写了下面的代码:

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 

我认为这个代码有很多问题,但是在执行的时候却失败了。打开,说: "Provider cannot be found. It may not be properly installed" 。 之后,我发现我需要下载并安装ORAOLEDB.dll。 我通过安装ORAOledb11.dll(我已经尝试了32位和64位,你的机器是64位)。 我已经通过执行regsvr32 OraOLEDB11.dll来安装它。

不幸的是,问题一直存在。 那么,解决这个问题的步骤是什么呢? 我可以以某种方式确保,Oraoledb已正确安装在我的机器上?

任何提示将非常感谢。

32位操作系统

我设法通过从Oracle官方网站Oracle10g Provider for OLE DB版本10.1.0.4.0下载Oracle OLEDB提供程序,在Windows XP虚拟机上运行 。 当前工作链接 OLEDB用于较旧的操作系统(32位)

但是请注意,它会replaceJDK和JRE到更低的版本(可以通过使用configurationxml – products.xml来防止 – 我没有足够的心理健康药水,所以我完全安装了)。 之后,您需要删除环境variables中的引用,因为它可能会影响其他程序。 安装完成后,我用regsvc32注册了OraOLEDBxx.dll

我用excel 2003连接到oracle db 11G. 🙂

连接string

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

 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版本来实现这个function。 之后,我确实下载了32位版本,和以前一样。 安装时请按照下载的归档文件夹中的安装说明进行操作

Oracle网站上ODAC的当前工作链接

您可以通过创build一个结尾.udl文本文件,然后closures并打开文件来确保您的连接string是准确的。 系统会提示用户界面连接到服务器。 input您的信息并testing连接。 然后如果你的连接正在closures文件。 以文本格式打开该文件,并将连接string复制到您的代码中。 还要确保为ADOselect了参考库。 这条线看起来不正确:

 Data Source=host:port:sid 

下面是我用来从文本中提取sql并将结果提取到文本的示例。

  Public Function ObjectConnect(AID As String, APswd As String) ObjectConnect = "Provider=ORAOLEDB;Password=" & APswd & ";Persist Security Info=True;User ID=" & AID & ";Data Source=(nameofserverConn)" End Function Sub RunSQL() Dim strConn As String Dim Query As String Dim txt As Object Dim ns As ADODB.Connection Dim rs As ADODB.Recordset Dim txtfile As Object Dim f As ADODB.Field Dim myFileSystemObject As Object Dim txtN As String Dim DL As String FName1 = "" Query = "" txtStrngX = "" Set ns = New ADODB.Connection Set rs = New ADODB.Recordset ns.ConnectionTimeout = 99000 ns.CommandTimeout = 99000 ns.Open ObjectConnect('UserID', 'Password') 'this is a public function w. userform for people to enter ID and Password. With rs .ActiveConnection = ns 'however you're writing the sql it would go here. .Open Query End With If rs.State <> 0 Then DL = Sheet1.Cells(2, 2) RecordsetToText rs:=rs, FullPath:=txtN, ValueDelimiter:=DL 'this is a sub function that writes to a text file. Didn't include the code but left this. Here you would want to do something with the recordset. End If On Error Resume Next rs.Close Set rs = Nothing End Sub