Ruby 连接到 SQL Server

发布于 2024-10-11 18:08:30 字数 1892 浏览 4 评论 0原文

我在将 Ruby 连接到 Microsoft SQL Server 时遇到问题。我运行的是 Mac OS X,但目标环境是 Ubuntu Linux。

这是我尝试过的:

  • 安装 unixODBC
  • 安装 免费TDS
    • 使用选项--with-unixodbc=/usr/local/etc --with-tdsver=8.0

然后我将这些文件放在 / usr/local/etc:

  • odbc.ini
  • odbcinst.ini
  • freetds.conf

我在odbcinst.ini 文件添加到我的 ODBC 驱动程序文件中,如下所示:

;
; odbcinst.ini
;
;
[FreeTDS]
Driver = /usr/local/lib/libtdsodbc.so

然后我在 freetds.conf 文件中配置服务器,如下所示:

# Aries database server (SQL Server 2008)
[aries-db1]
        host = xx.xx.xx.xx
        port = 1433
        tds version = 8.0

最后,我在 freetds.conf 文件中添加了 ODBC DSN code>odbc.ini 文件如下:

[aries-db1]
Driver      =   FreeTDS
Description =   ODBC Connection via FreeTDS
Trace       =   1
Servername  =   aries-db1
Database    =   MY_DB
UID         =   user1
PWD         =   pass1

我可以验证我的服务器是否在线并且端口是否打开(通过 telnet 和 yougetsignal.com 端口检查)。

作为测试,我这样做了:

tsql -S aries-db1 -U user1 -P pass1

它似乎连接得很好。传递无效值会导致预期的错误。

最后我的问题是:

如何将其扩展到 Ruby?到目前为止我所尝试的一切都不起作用。我尝试了这样的 Sequel:

require 'sequel'
Sequel.connect('aries-db1')['select * from foo'].all

我收到这样的错误:

Sequel::DatabaseConnectionError: ODBC::Error: S1000 (0) [unixODBC][FreeTDS][SQL Server]Unable to connect to data source

这告诉我它正在正确找到我的驱动程序配置,但由于某种原因无法连接。

我也尝试过这样的 DBI:

DBI.connect('DBI:ODBC:aries-db1')

并且我收到类似的错误。

有什么建议吗?我觉得我已经非常接近了,但不知道接下来要尝试什么来解决这个问题。

I'm having troubles connecting Ruby to Microsoft SQL Server. I'm running Mac OS X, but the target environment is Ubuntu Linux.

Here's what I've tried:

  • Install unixODBC
  • Install FreeTDS
    • used the options --with-unixodbc=/usr/local/etc --with-tdsver=8.0

I then had these files in /usr/local/etc:

  • odbc.ini
  • odbcinst.ini
  • freetds.conf

I added a reference to the FreeTDS driver in the odbcinst.ini file to my ODBC driver file like this:

;
; odbcinst.ini
;
;
[FreeTDS]
Driver = /usr/local/lib/libtdsodbc.so

Then I configured the server in the freetds.conf file like this:

# Aries database server (SQL Server 2008)
[aries-db1]
        host = xx.xx.xx.xx
        port = 1433
        tds version = 8.0

And finally I added the ODBC DSN in the odbc.ini file like this:

[aries-db1]
Driver      =   FreeTDS
Description =   ODBC Connection via FreeTDS
Trace       =   1
Servername  =   aries-db1
Database    =   MY_DB
UID         =   user1
PWD         =   pass1

I can verify that my server is online and the port is open (via telnet & yougetsignal.com port check).

As a test, I did this:

tsql -S aries-db1 -U user1 -P pass1

And it seemed to connect just fine. Passing in invalid values resulted in expected errors.

So finally to my question:

How do I extend this to Ruby? Nothing I've tried so far has worked. I tried Sequel like this:

require 'sequel'
Sequel.connect('aries-db1')['select * from foo'].all

And I get an error like this:

Sequel::DatabaseConnectionError: ODBC::Error: S1000 (0) [unixODBC][FreeTDS][SQL Server]Unable to connect to data source

Which tells me it is finding my driver configuration correctly, but for some reason cannot connect.

I also tried DBI like this:

DBI.connect('DBI:ODBC:aries-db1')

And I get a similar error.

Any suggestions? I feel like I'm very close, but am not sure what to try next to troubleshoot this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

败给现实 2024-10-18 18:08:30

看看
https://github.com/rails-sqlserver/tiny_tds
TinyTds - 使用 DB-Library 的现代、简单和快速的 Ruby FreeTDS 库

它易于安装和使用

Have a Look at
https://github.com/rails-sqlserver/tiny_tds
TinyTds - A modern, simple and fast FreeTDS library for Ruby using DB-Library

Its easy to install and easy to use

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文