# Connection String to MS SQL Server 2005

**URL:** <https://www.ruby-forum.com/t/connection-string-to-ms-sql-server-2005/110738>\
**Category:** Ruby\
**Created:** [July 30, 2007, 2:09pm UTC](https://www.ruby-forum.com/t/connection-string-to-ms-sql-server-2005/110738 "2007-07-30T14:09:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanivk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/858c86/32.png) [@vanivk](https://www.ruby-forum.com/u/vanivk)\
**Post date:** [July 30, 2007, 2:09pm UTC](https://www.ruby-forum.com/t/connection-string-to-ms-sql-server-2005/110738/1 "2007-07-30T14:09:33Z")

</div>

Hello all,

I have been to trying to connect to SQL Server 2005 from ruby code.  
Following is the code written:

* * *

require ‘dbi’

driver = ‘ODBC’  
server = “PRODSK0322”  
database = ‘TestDB’  
user = ‘sa’  
password = ‘Sa123’

connection\_string = “DBI:ODBC:Driver={SQL  
Server};Server=#{server};Database=#{database};Uid=#{user};Pwd=#{password}”

begin

# connect

db = DBI.connect(connection\_string, user, password)

# get data and print

row = db.select\_one(“SELECT TrnEmployees”)  
puts "result: " + row[0].to\_s  
rescue DBI::DatabaseError =\> e  
puts “Error #{e.errstr}”  
ensure

# disconnect from server

db.disconnect if db  
end

* * *

I am getting an error saying :  
“Error IM002 (0) [Microsoft][ODBC Driver Manager] Data source name not  
found and no default driver specified”

Can any one tell me where I am going wrong?  
Is there anything I need to install or am I missing anything in includes  
or requires?

Thanks,  
Vani

---

<div class="post-metadata">

**Author:** ![vanivk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/858c86/32.png) [@vanivk](https://www.ruby-forum.com/u/vanivk)\
**Post date:** [July 30, 2007, 2:26pm UTC](https://www.ruby-forum.com/t/connection-string-to-ms-sql-server-2005/110738/2 "2007-07-30T14:26:39Z")

</div>

> Following is the code written:
> 
> rescue DBI::DatabaseError =\> e  
> "Error IM002 (0) [Microsoft][ODBC Driver Manager] Data source

If you’re attempting to connect via ODBC, you need to set up the ODBC  
source  
separately.

On Linux, you would probably install FreeTDS and unixODBC, setting up  
/etc/odbc.ini with an entry like this:

[My\_SQL2005\_Server]  
Driver = FreeTDS  
Description = SQL2005  
Trace = No  
Server = PRODSK0322  
Port = 1433  
Database = TestDB

On Windows, I’m not sure how to set up the ODBC sources.

Then you can connect with:

dbh = DBI.connect(“DBI:ODBC:My\_SQL2005\_Server”, “sa”, “!Sa123”)

---

<div class="post-metadata">

**Author:** ![vanivk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/858c86/32.png) [@vanivk](https://www.ruby-forum.com/u/vanivk)\
**Post date:** [July 30, 2007, 2:44pm UTC](https://www.ruby-forum.com/t/connection-string-to-ms-sql-server-2005/110738/3 "2007-07-30T14:44:20Z")

</div>

Hello Felix,  
I need to establish a connection to SQL Server 2005 on Windows.  
Thanks,  
Vani

Felix W. wrote:

> > Following is the code written:
> > 
> > rescue DBI::DatabaseError =\> e  
> > "Error IM002 (0) [Microsoft][ODBC Driver Manager] Data source
> 
> If you’re attempting to connect via ODBC, you need to set up the ODBC  
> source  
> separately.
> 
> On Linux, you would probably install FreeTDS and unixODBC, setting up  
> /etc/odbc.ini with an entry like this:
> 
> [My\_SQL2005\_Server]  
> Driver = FreeTDS  
> Description = SQL2005  
> Trace = No  
> Server = PRODSK0322  
> Port = 1433  
> Database = TestDB
> 
> On Windows, I’m not sure how to set up the ODBC sources.
> 
> Then you can connect with:
> 
> dbh = DBI.connect(“DBI:ODBC:My\_SQL2005\_Server”, “sa”, “!Sa123”)

---

<div class="post-metadata">

**Author:** ![vanivk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/858c86/32.png) [@vanivk](https://www.ruby-forum.com/u/vanivk)\
**Post date:** [August 3, 2007, 5:26pm UTC](https://www.ruby-forum.com/t/connection-string-to-ms-sql-server-2005/110738/4 "2007-08-03T17:26:05Z")

</div>

Felix W. wrote:

> > Vani  
> > up the ODBC
> > 
> > > Server = PRODSK0322  
> > > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> Try [http://support.microsoft.com/kb/305599](http://support.microsoft.com/kb/305599)
> 
> That might help creating the DSN you refer to in “DBI:ODBC:DSN”.

Not sure whether this is acceptable for posting , but there is a site I  
found which had a good snippet for connecting to SQL Server if you are  
on a windows machine. It uses ADO and worked first go. there are also  
other useful SQL Server snippets

[http://snippets.dzone.com/posts/show/3906](http://snippets.dzone.com/posts/show/3906)

---

<div class="post-metadata">

**Author:** ![vanivk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/858c86/32.png) [@vanivk](https://www.ruby-forum.com/u/vanivk)\
**Post date:** [July 30, 2007, 2:58pm UTC](https://www.ruby-forum.com/t/connection-string-to-ms-sql-server-2005/110738/5 "2007-07-30T14:58:43Z")

</div>

> Vani  
> up the ODBC
> 
> > Server = PRODSK0322  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Try [http://support.microsoft.com/kb/305599](http://support.microsoft.com/kb/305599)

That might help creating the DSN you refer to in “DBI:ODBC:DSN”.
