Ruby on Rails
HowtoConnectToMicrosoftSQLServerFromRailsOnDebian (Version #7)

Installing

If you want to connect to a MSSQL-Server from Debian Sarge you’ll need the following packages:


tdsodbc 0.63-3 ODBC driver for connecting to MS SQL libiodbc2 3.52.2-3 iODBC Driver Manager libdbi-ruby 0.0.23-2 Database Independent Interface for Ruby libdbd-odbc-ruby 0.0.23-2 Ruby/DBI ODBC driver for Ruby 1.8 libodbc-ruby 0.994-1 ODBC Binding for ruby1.8

Configure FreeTDS

After installing all this packages you need to configure freetds by adding the following lines:

/etc/freetds/freetds.conf


[YourTDSDataSource] host = 192.168.xxx.xxx port = 1433 tds version = 8.0

If you need umlauts, you can also add
the line “client charset = ISO-8859-1”.

Configure iODBC

The odbc driver configuration should look like this:

/etc/odbc.ini


[YourDSN] Driver = /usr/lib/odbc/libtdsodbc.so ServerName = YourTDSDataSource

Testing ODBC

Now you have completed the configuration of your ODBC-Datasources and can test them:


  1. irb1.8
    irb(main):001:0> require ‘dbi’
    => true
    irb(main):002:0> conn = DBI.connect(‘dbi:ODBC:YourDSN’, ‘YourUsername’, ’YourPassword’)
    => #<DBI::DatabaseHandle:0×403eede0 @trace_output=#, @trace_mode=
    2, @handle=#<DBI::DBD::ODBC::Database:0×403eed18 @attr={}, @handle=#>>
    irb(main):003:0> conn.execute(‘SELECT 1’)
    irb(main):003:0> quit

Integrating in Rails

You can edit the datasource in database.yml:


development: adapter: sqlserver mode: odbc dsn: YourDSN username: YourUsername password: YourPassword database: YourDatabase

Installing

If you want to connect to a MSSQL-Server from Debian Sarge you’ll need the following packages:


tdsodbc 0.63-3 ODBC driver for connecting to MS SQL libiodbc2 3.52.2-3 iODBC Driver Manager libdbi-ruby 0.0.23-2 Database Independent Interface for Ruby libdbd-odbc-ruby 0.0.23-2 Ruby/DBI ODBC driver for Ruby 1.8 libodbc-ruby 0.994-1 ODBC Binding for ruby1.8

Configure FreeTDS

After installing all this packages you need to configure freetds by adding the following lines:

/etc/freetds/freetds.conf


[YourTDSDataSource] host = 192.168.xxx.xxx port = 1433 tds version = 8.0

If you need umlauts, you can also add
the line “client charset = ISO-8859-1”.

Configure iODBC

The odbc driver configuration should look like this:

/etc/odbc.ini


[YourDSN] Driver = /usr/lib/odbc/libtdsodbc.so ServerName = YourTDSDataSource

Testing ODBC

Now you have completed the configuration of your ODBC-Datasources and can test them:


  1. irb1.8
    irb(main):001:0> require ‘dbi’
    => true
    irb(main):002:0> conn = DBI.connect(‘dbi:ODBC:YourDSN’, ‘YourUsername’, ’YourPassword’)
    => #<DBI::DatabaseHandle:0×403eede0 @trace_output=#, @trace_mode=
    2, @handle=#<DBI::DBD::ODBC::Database:0×403eed18 @attr={}, @handle=#>>
    irb(main):003:0> conn.execute(‘SELECT 1’)
    irb(main):003:0> quit

Integrating in Rails

You can edit the datasource in database.yml:


development: adapter: sqlserver mode: odbc dsn: YourDSN username: YourUsername password: YourPassword database: YourDatabase