简体   繁体   中英

How to connect from ruby to MS Sql Server

I'm trying to connect to the sql server 2005 database from *NIX machine:

I have the following configuration: Linux 64bit

ruby -v ruby 1.8.6 (2007-09-24 patchlevel 111) [x86_64-linux] important gems: dbd-odbc (0.2.4) dbi (0.4.1) active record sql server adapter - as plugin

ruby-odbc 0.9996 (installed without any options.) unixODBC is installed freeTDS is installed

cat /etc/odbcinst.ini

  [FreeTDS]
     Description     = TDS driver (Sybase/MS SQL)
     Driver          = /usr/lib/libtdsodbc.so
     Setup           = /usr/lib/odbc/libtdsS.so
     CPTimeout       =
     CPReuse         =
     FileUsage       = 1

DSN:

DRIVER=FreeTDS;TDS_Version=8.0;SERVER=XXXX;DATABASE=XXX;Port=1433;uid=XXX;pwd=XXXX;"

or

DRIVER=/usr/lib/libtdsodbc.so;TDS_Version=8.0;SERVER=XXXX;DATABASE=XXX;Port=1433;uid=XXX;pwd=XXXX;"

I receive the following error:

>>ActiveRecord::Base.sqlserver_connection({"mode"=>"ODBC", "adapter"=>"sqlserver", "dsn"=>my_dns)
DBI::DatabaseError: IM002 (0) [unixODBC][Driver Manager]Data source name not found, and no default driver specified
    from /usr/lib/ruby/1.8/DBD/ODBC/ODBC.rb:95:in `connect'
    from /usr/lib/ruby/1.8/dbi.rb:424:in `connect'
    from /usr/lib/ruby/1.8/dbi.rb:215:in `connect'
    from /opt/ublip/rails/current/vendor/plugins/activerecord-sqlserver-adapter/lib/active_record/connection_adapters/sqlserver_adapter.rb:47:in `sqlserver_connection'

It looks like ODBC unable to find appropriate ODBC driver, but I have no ideas why.

I had a problem with /usr/lib/libtdsodbc.so which is empty in default debian package free-tds dev, but i solved it with remove broken package and installation from sources.

Will appreciate any thought!

Note: I'm albe to connect using the same steps on mac 10.5

This looks close, and I've gotten the same type of setup to work on Ubuntu 8.04 (with Ruby 1.8.6.) Since this is on Debian, it should be the similar.

These packages should be installed: unixodbc unixodbc-dev freetds-dev libdbd-odbc-ruby tdsodbc

/etc/odbcinst.ini should have something like this:

[FreeTDS]
Description = FreeTDS unixODBC Driver
Driver = /usr/lib/odbc/libtdsodbc.so
Setup = /usr/lib/odbc/libtdsodbc.so
UsageCount = 1

And /etc/freetds/freetds.conf: (where XXX is the database name and NNN... is the IP of the server):

[XXX]
  host = NNN.NNN.NNN.NNN
  tds version = 8.0
  client charset = UTF-8
  text size = 262144
  port = 1433

Then to connect:

ActiveRecord::Base.sqlserver_connection({"mode"=>"ODBC", 
  "adapter"=>"sqlserver", 
  "dsn"=>"DRIVER=FreeTDS;SERVERNAME=XXX;DATABASE=XXX;uid=XXX;pwd=XXX"
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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