user1361575
user1361575

Reputation: 11

How to access SQL Server 2008 from Ruby

I'm using WATIR and Ruby 1.9.3 to test web applications and I need to connect to Microsoft SQL Server Management Studio 2008 (Windows 7 64-bit). I already installed "DBI and DBD-ODBC" gems. I have also installed the ODBC Data Source, which I named 'BUILD'. I was following the next steps, but unfortunately I'm getting a syntax error message. This is what I tried:

require 'dbi'
conn = DBI.connect('DBI:ODBC:BUILD', 'username', 'password')conn.connected?

Which gives me this error message:

SyntaxError: (irb):2: syntax error, unexpected tIDENTIFIER, expecting $end
...'username', 'password')conn.connected?
...                           ^
    from C:/Ruby193/bin/irb:12:in `<main>'

I honestly don't know what I'm doing wrong. I searched online for a solution, but apparently I couldn't find any answer that it can help me. I'll appreciate your help thanks!

Upvotes: 1

Views: 2007

Answers (2)

Josh
Josh

Reputation: 5721

I think you have a syntax error in this part:

conn = DBI.connect('DBI:ODBC:BUILD', 'username', 'password')conn.connected?

Try running this instead:

conn = DBI.connect('DBI:ODBC:BUILD', 'username', 'password') #==> sets up the connection

conn.connected?  #==>  true if it is working

You had a method call followed immediately by a variable which is why your compiler is complaining that you never ended the method before you called conn again.

Upvotes: 1

Klaus
Klaus

Reputation: 902

For easy access to a SQL Server you should have a look at TinyTds https://github.com/rails-sqlserver/tiny_tds

Upvotes: 1

Related Questions