如何连接Excel到Oracle数据库?

huangapple go评论69阅读模式
英文:

How to connect Excel to Oracle database?

问题

我正在尝试使用VBA将Excel连接到我的Oracle数据库。

Sub dbConnect()

    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Set con = New ADODB.Connection
    Set rs = New ADODB.Recordset

    strCon = "Driver={Microsoft ODBC for Oracle}; " & _
    "CONNECTSTRING=(description=(address=(protocol=tcp)(host=mydb.domain.com)(port=1522))(connect_data=(sid=mydb))); uid=user; pwd=pw;"
    con.Open (strCon)

End Sub

我收到一个错误消息。

[Microsoft][ODBC Driver Manager] 数据源名称未找到,未指定默认驱动程序

我知道从其他问题来源中,问题很可能是使用了错误版本的DSN。

然而,我不明白需要做什么来修复它。

我的Windows是64位的,当我打开ODBC数据源管理员时,我看到以下内容(以及其他内容):

  • 名称:Excel Files,平台:64位,驱动程序:Microsoft Excel Driver

    用户DSN

  • 名称:mydb,平台:32位,驱动程序:oracle in ORA121020_x86

    系统DSN

我该怎么做才能修复它?连接字符串和DSN之间有什么关系?我应该更改其中一个DSN的版本吗?如果是的话,我应该如何做?

编辑:
连接字符串是从Oracle SQL Developer中的数据库连接复制的,在那里我可以访问数据库。

英文:

I am trying to connect Excel to my Oracle database using VBA.

<!-- language: vba -->

Sub dbConnect()

    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Set con = New ADODB.Connection
    Set rs = New ADODB.Recordset
    
    strCon = &quot;Driver={Microsoft ODBC for Oracle}; &quot; &amp; _
    &quot;CONNECTSTRING=(description=(address=(protocol=tcp)(host=mydb.domain.com)(port=1522))(connect_data=(sid=mydb))); uid=user; pwd=pw;&quot;
    con.Open (strCon)

End Sub

I get an error.

> [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

I know from other questions and sources that the problem is most likely using the wrong version of the DSN.

However, I don't understand what I need to do to fix it.

My Windows is 64-bit, when I open the ODBC Data Source Administrator I see the following (among others):

  • Name: Excel Files, Platform: 64-bit, Driver: Microsoft Excel Driver

    User DSN

  • Name: mydb, Platform: 32-bit, Driver: oracle in ORA121020_x86

    under System DSN

What can I do to fix it? How do the connection string and the DNSs relate to each other? Should I change the version of one of the DNS and if yes how do I do that?

Edit:
The connection string is copied from the db connection in Oracel SQL Developer where I can access the db.

答案1

得分: 1

我成功修复了它。我的Excel是64位的,所以为了连接到数据库,我不得不创建一个与我的连接一样是64位的系统DNS。为了做到这一点,我必须进入/Windows/system32文件夹,选择odbcad32文件,在系统DNS下添加一个新的DNS,使用我下载的64位驱动程序。假设我将那个DNS命名为abc。

然后,我还更改了上面代码中可以看到的连接字符串为:

strCon = "Data Source=abc;User=user;Password=pw"

根据这里,我在上面的帖子中使用的连接字符串不需要DNS,所以我不知道为什么它不起作用,但是在创建了一个新的DNS后,我切换到了指定DNS的新连接字符串。

嗯,经过几个小时后,我成功连接到了我的数据库。

快速的侧面说明:如上所述,我编辑了/Windows/system32中的odbcad32文件。这是用于64位DNS的文件。在/Windows/SysWOW64文件夹中有一个完全相同名称的文件,用于管理32位DNS,所以如果你遇到类似的问题,要注意编辑哪个文件。

英文:

Ok I managed to fix it. My excel is 64 bit so in order to connect to the db, I had to create a System DNS for my connection that is also 64 bit. In order to do that, I had to go to the /Windows/system32 folder, choose the file odbcad32 and under system DNS add a new DNS with a 64 bit driver I had to download. Lets say I named that DNS abc

I then also changed the connection string that you can see in the above code to

strCon = &quot;Data Source=abc;User=user;Password=pw&quot;

According to this the connection string I used in the post above does not need a DNS, so I don't know why it didn't work, however after creating a new DNS as just described I switched to the new connection string that specifies a DNS.

And voila, after only several hours I was able to connect to my db.

Quick sidenote: As mentioned above, I edited the obcad32 file in /Windows/system32. This is the file for 64 bit DNS. There is a file with the exact same name in the folder /Windows/SysWOW64 that manages the 32 bit DNS, so if you have a similar problem as I had, pay attention to which file you edit.

huangapple
  • 本文由 发表于 2020年1月3日 20:16:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定