web 2.0

Using Ruby with SQL Server

Update: I have created an up-to-date tutorial that shows how to use Ruby on Rails with SQL 2008. It is written from the perspective of a .NET Developer and tries to draw comparisons between ASP.NET MVC with LINQ and Ruby on Rails with ActiveRecord.


In the computer world most people seem to be die-hard Windows or die-hard Linux. From a programmers perspective, each platform seems to have a core set of technologies that are associated with it. If you are a web developer working on the Windows platform you are probably going to build solutions using SQL Server and .NET. If you are working in a Linux shop you may be more inclined to use MySQL and Ruby.

In my case, I like Windows but I am intrigued by Ruby. I want to write Ruby apps that run on Windows and talk to SQL Server. So I started the challenge the same way most people do. I Googled for some sample code. Unfortunately, most of the snippets I found on the Internet were based on Linux and MySQL. Now I could have copped out and used IronRuby (Microsoft's interpretation of Ruby) but the thought of it made me feel cheap. I really wanted to use the language in its truest form. Eventually I figured out the process. Here it is:

  1. Install Ruby on Windows. There is a one-click installer which you can download here.
  2. Install the Ruby-DBI package. There is a great outline of the process here. The hardest part is making sure that you put the ADO.rb file in the proper folder.

Once the installation is done you can open up your favorite editor and start writing some code. The first step is to include the DBI module. The Ruby DBI module is the library that you use to access the database server. Now it is just a matter of creating your database connection and executing some SQL. The end result is this chunk of code:

require 'dbi'

oConn = DBI.connect('DBI:ADO:Provider=SQLNCLI;Data Source="sqlserver01";Integrated Security=SSPI;Initial Catalog=master')
sth = oConn.execute("SELECT name * FROM sys.databases")
rows = sth.fetch_all
col_names = sth.column_names
sth.finish
DBI::Utils::TableFormatter.ascii(col_names, rows)

Here are the results:

+-----------------------------+ name +-----------------------------+
master
tempdb
model
msdb
AdventureWorks

 

Not bad for a few minutes of tinkering! Now that the database connectivity issue is solved I can start experimenting with ActiveRecord. I also discovered the rubydotnet project recently which gives you the ability to utilize the .NET framework from Ruby. I do not know if this has any real world applications yet but it just looks like it would be fun to try.

Comments

Waysys LLC , on 12/17/2009 2:29:56 PM Said:

Waysys LLC

Readers of this article might find these instructions useful:

http://waysysweb.com/qa/odbc.html

Anonymous , on 12/17/2009 2:29:56 PM Said:

Anonymous

To get this to work properly...
The ado.rb file is in the .2 version of DBI.

William Shaffer , on 12/17/2009 2:29:56 PM Said:

William Shaffer

This is a potentially useful article except for two things:
1) the link to installing the Ruby-DBI package no longer exists and therefore, it is really hard to know where to find or put the ADO driver.
2) There is not explanation of the argument to the connect method: will those same parameters work on my computer as they did on yours?

Anonymous , on 12/17/2009 2:29:56 PM Said:

Anonymous

ADO.rb does not appear to be contained in
dbi-0.4.2.zip...

Michael Ceranski , on 12/17/2009 2:29:56 PM Said:

Michael Ceranski

Waysys...Very nice article. Thanks for the link.

Michael Ceranski , on 12/17/2009 2:29:56 PM Said:

Michael Ceranski

Sorry...my article is a bit dated. You have to grab the file from the dbi-0.2.2 download. They must have deprecated the library in the new dbi package.

Good luck!

Michael Ceranski , on 12/17/2009 2:29:56 PM Said:

Michael Ceranski

Yes, you are correct. In a previous comment I referenced version 0.2.2 or the dbi library.

Michael Ceranski , on 12/17/2009 2:29:56 PM Said:

Michael Ceranski

I am not sure why the link did not work for you. It opened fine for me. Anyway, here is the link for the dbi zip file: rubyforge.org/frs/download.php/60191/dbi-0.4.2.zip and here is a link on how to install it blog.adsdevshop.com/.../

As far as the connection string goes, you will need to modify the datasource to match the name of the SQL instance you are trying to access. If you are using Windows Authentication the Integrated Security=SSPI should be fine. If you want to use SQL accounts then you will need to specify the UserID and Password as parameters in the connection string.

Good Luck!

Code Capers , on 1/12/2010 9:17:54 PM Said:

trackback

A Ruby on Rails Tutorial for .NET Developers

A Ruby on Rails Tutorial for .NET Developers

Comments are closed