简体   繁体   English

尝试使用 VBA 连接到 Oracle DB 服务器

[英]Trying to connect to an Oracle DB Server using VBA

As the title states I'm trying to connect to an oracle DB for a vba script.正如标题所述,我正在尝试连接到 oracle DB 以获取 vba 脚本。

I was using this article here on how to do it: https://blogs.perficient.com/2015/12/17/connectcompare-databases-excel-macro/我在这里使用了这篇关于如何做的文章: https : //blogs.perficient.com/2015/12/17/connectcompare-databases-excel-macro/

I just have very basic code for connecting and I'm getting an error that says "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified"我只有非常基本的连接代码,但收到一条错误消息,提示"[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified"

My code is:我的代码是:

Private Sub CommandButton1_Click()
    Dim dbConnection As ADODB.Connection
    Dim recordSet As ADODB.recordSet
    Set dbConnection = New ADODB.Connection
    Set recordSet = New ADODB.recordSet
    conString = "Driver = {Microsoft ODBC for Oracle};DBQ=####;Uid=<####>;Pwd=<####>;"
    dbConnection.Open (conString)

End Sub

One thing I am not sure on is what exactly is DBQ is.我不确定的一件事是 DBQ 到底是什么。 I have set DBQ to the name of the database I'm interested in. Not sure if I'm setting that parameter properly.我已将 DBQ 设置为我感兴趣的数据库的名称。不确定我是否正确设置了该参数。

Thank you and all help is appreciated it.谢谢你,感谢所有帮助。

First question: Did you install an Oracle Client on your machine?第一个问题:您是否在您的机器上安装了 Oracle 客户端?

According to your connection string you use the Microsoft ODBC for Oracle driver.根据您的连接字符串,您使用Microsoft ODBC for Oracle驱动程序。 Connection string must be like this:连接字符串必须是这样的:

Driver={Microsoft ODBC for Oracle};Server=myServerAddress;Uid=myUsername;Pwd=myPassword;

Note, the connection string is slightly different to the Oracle ODBC driver, see ODBC Driver Microsoft vs.ODBC Driver Oracle请注意,连接字符串与Oracle ODBC 驱动程序略有不同,请参阅ODBC Driver Microsoft vs.ODBC Driver Oracle

Second question: Is your Excel 32-bit or 64-bit?第二个问题:你的Excel是32位还是64位?

The Oracle Client and the ODBC driver must be the same architecture (ie 32-bit vs. 64-bit) as your Excel. Oracle 客户端和 ODBC 驱动程序必须与 Excel 具有相同的体系结构(即 32 位与 64 位)。 The ancient Microsoft ODBC for Oracle driver exist only for 32-bit.古老的Microsoft ODBC for Oracle驱动程序仅适用于 32 位。 If your Excel is 64-bit then you have to use the Oracle ODBC driver.如果您的 Excel 是 64 位,那么您必须使用 Oracle ODBC 驱动程序。

Another note, the default Oracle Instant client does not include the ODBC driver, you have to download and install the "ODBC Package", from Oracle Instant Client Downloads另请注意,默认的 Oracle Instant Client 不包含 ODBC 驱动程序,您必须从Oracle Instant Client Downloads下载并安装“ODBC Package”

There is a comment above to use this format which I believe is correct "DSN={MyDataSource};UID={MyUserID};PWD={MyPassword};SERVER={MyOracleServer}"上面有一条评论使用我认为正确的格式“DSN={MyDataSource};UID={MyUserID};PWD={MyPassword};SERVER={MyOracleServer}”

From the Microsoft documentation and here来自Microsoft 文档此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM