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:
--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.
In Ruby we can connect to database using DBI module. DBI module provides Database Independent Interface to access the data source. So, using DBI, the underlying data source can be any database like Oracle, MySQL, Postgres etc, however the API remains the same.
Start the SQL Server, in the dialog window for the Server name enters the name of the instance that you want to connect with. From the Authentication drop down box, select the SQL Server Authentication and for the field Login and the Password enter your credentials then click the Connect button.
In SQLite Ruby module, first we prepare the SQL statement with the prepare method. The SQL string is sent to the database engine, which checks the statement validity, syntax and in some databases also the user permissions to perform certain queries. If all is OK, a statement object is returned to the Ruby script.
TinyTDS - A modern, simple and fast FreeTDS library for Ruby using DB-Library. Developed for the ActiveRecord SQL Server adapter.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With