使用 Ruby、ODBC 和 FreeTDS 从 Mac 连接到 MS SQL Server 2005
我在从这个非常简单的 Ruby 应用程序连接到 SQL Server 2005 安装时遇到问题:
#!/usr/bin/env ruby
require 'rubygems'
require 'sequel'
Sequel.odbc('dev04')['select top 1 * from users'].all
dev04
DSN 指向我的 odbc.ini
文件条目:
[dev04]
Driver = FreeTDS
Description = ODBC connection via FreeTDS
Trace = 1
Servername = dev04
Database = Dev04
UID = uDev04
PWD = pwdDev04
FreeTDS< /code> 安装在 odbcinst.ini 中,它指向
freetds.conf
中的此配置:
[dev04]
host = hm602.mycompany.com
port = 1433
tds version = 7.0
这有效:
carlos$ tsql -S dev04 -U uDev04 -P pwdDev04
locale is "en_GB.UTF-8"
locale charset is "UTF-8"
1> exit
carlos$
这也是如此:
carlos$ iodbctest "DSN=dev04;UID=uDev04;PWD=pwdDev04"
iODBC Demonstration program
This program shows an interactive SQL processor
Driver Manager: 03.52.0406.1211
Driver: 0.82 (libtdsodbc.so)
SQL>exit
Have a nice day.
carlos$
但是,当我运行该 Ruby 脚本时,我收到此错误:
carlos$ ruby mssql.rb
/Library/Ruby/Gems/1.8/gems/sequel-3.4.0/lib/sequel/adapters/odbc.rb:37:in `initialize': ODBC::Error 01000 (20002) [FreeTDS][SQL Server]Adaptive Server connection failed (Sequel::DatabaseConnectionError)
from /Library/Ruby/Gems/1.8/gems/sequel-3.4.0/lib/sequel/adapters/odbc.rb:37:in `connect'
from /Library/Ruby/Gems/1.8/gems/sequel-3.4.0/lib/sequel/adapters/odbc.rb:37:in `connect'
from /Library/Ruby/Gems/1.8/gems/sequel-3.4.0/lib/sequel/database.rb:92:in `initialize'
...
I了解由于 TDS 协议版本差异可能会出现“Adaptive Server 连接失败”错误。我检查了日志,它们似乎都使用正确的版本(7.0),所以我完全不知道为什么会发生这种情况。
有什么指点吗?
I'm having trouble connecting to a SQL Server 2005 install from this very simple Ruby app:
#!/usr/bin/env ruby
require 'rubygems'
require 'sequel'
Sequel.odbc('dev04')['select top 1 * from users'].all
The dev04
DSN points to my odbc.ini
file entry:
[dev04]
Driver = FreeTDS
Description = ODBC connection via FreeTDS
Trace = 1
Servername = dev04
Database = Dev04
UID = uDev04
PWD = pwdDev04
FreeTDS
is installed in odbcinst.ini and it points to this configuration in freetds.conf
:
[dev04]
host = hm602.mycompany.com
port = 1433
tds version = 7.0
This works:
carlos$ tsql -S dev04 -U uDev04 -P pwdDev04
locale is "en_GB.UTF-8"
locale charset is "UTF-8"
1> exit
carlos$
And so does this:
carlos$ iodbctest "DSN=dev04;UID=uDev04;PWD=pwdDev04"
iODBC Demonstration program
This program shows an interactive SQL processor
Driver Manager: 03.52.0406.1211
Driver: 0.82 (libtdsodbc.so)
SQL>exit
Have a nice day.
carlos$
When I run that Ruby script, though, I get this error:
carlos$ ruby mssql.rb
/Library/Ruby/Gems/1.8/gems/sequel-3.4.0/lib/sequel/adapters/odbc.rb:37:in `initialize': ODBC::Error 01000 (20002) [FreeTDS][SQL Server]Adaptive Server connection failed (Sequel::DatabaseConnectionError)
from /Library/Ruby/Gems/1.8/gems/sequel-3.4.0/lib/sequel/adapters/odbc.rb:37:in `connect'
from /Library/Ruby/Gems/1.8/gems/sequel-3.4.0/lib/sequel/adapters/odbc.rb:37:in `connect'
from /Library/Ruby/Gems/1.8/gems/sequel-3.4.0/lib/sequel/database.rb:92:in `initialize'
...
I understand the 'Adaptive Server connection failed' error can occur due to TDS protocol version discrepancies. I've checked the logs and they all seem to be using the correct version (7.0), so I'm at a complete loss as to why this could be happening.
Any pointers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我是续集维护者。不幸的是,我没有任何使用 Sequel 使用 FreeTDS 连接到 MSSQL 的个人经验。我知道其他人已经成功地使用了它。
您的错误消息表明底层 Ruby/ODBC 库存在问题,而不是 Sequel 本身。如果您可以直接使用 Ruby/ODBC 建立连接,那么使用 Sequel 进行连接应该不难。
抱歉,我无法提供更多帮助。
另外,虽然它与您当前的问题无关,但您应该在 Sequel.odbc 调用中使用 :db_type=>'mssql' 选项。
I'm the Sequel maintainer. Unfortunately, I don't have any personal experience using Sequel to connect to MSSQL using FreeTDS. I know other people have used it successfully though.
Your error message indicates a issue in the underlying Ruby/ODBC library, not in Sequel itself. If you can get a connection working using Ruby/ODBC directly, it shouldn't be hard to get it working with Sequel.
Sorry I can't be more help.
Also, while it is unrelated to your current issue, you should use the :db_type=>'mssql' option to the Sequel.odbc call.
有时,免费并非没有成本。
TDS 版本 7.0 对于 SQL Server 7.x 来说是正确的,并且在许多情况下可以连接到更高版本的 SQL Server,但它并不支持这些更高版本的所有特性或功能。
TDS 版本 7.1 对于 SQL Server 2000 (v8.x) 是正确的; 7.2 对于 SQL Server 2005 (v9.x) 是正确的; 7.3 对于 SQL Server 2008 (v10.x) 是正确的。由于微软的文档有些混乱,这些通常在 freetds.conf 中指定使用 SQL Server 版本,而不是 TDS 版本; SQL Server 2008 尚未完全支持;所以我会用
Or 进行测试...您可以尝试使用 像我雇主的这个商业驱动程序已经处理了很多令人头疼的问题。如果您确实选择了这条路线,我建议您还查看Rails ODBC 适配器的完整文档
免责声明:我为 OpenLink Software 工作,但不会直接从选择使用我们产品的任何人中受益。
Sometimes, free isn't without cost.
TDS Version 7.0 was correct for SQL Server 7.x, and it will connect in many cases to later versions of SQL Server -- but it does not support all features or functionality of those later versions.
TDS Version 7.1 is correct for SQL Server 2000 (v8.x); 7.2 is correct for SQL Server 2005 (v9.x); 7.3 is correct for SQL Server 2008 (v10.x). Due to some confusion in Microsoft's documentation, these are usually specified in the freetds.conf using the SQL Server version, not the TDS version; and SQL Server 2008 isn't fully supported yet; so I'd test with
Or ... you could try using a commercial driver like this one from my employer that has already handled much of the head-scratching. If you do go that route, I'd suggest you also review the full documentation for the Rails ODBC Adapter
ObDisclaimer: I work for OpenLink Software, but do not directly benefit from anyone choosing to use our products.