Forum: Ruby Connection String to MS SQL Server 2005

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ce66737ae6eccdd36108a1a6894cad37?d=identicon&s=25 Vani Kulkarni (vanivk)
on 2007-07-30 14:09
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
15a5043475dac9278ae75efb4c71f1f6?d=identicon&s=25 Felix Windt (Guest)
on 2007-07-30 14:26
(Received via mailing list)
> 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")
Ce66737ae6eccdd36108a1a6894cad37?d=identicon&s=25 Vani Kulkarni (vanivk)
on 2007-07-30 14:44
Hello Felix,
 I need to establish a connection to SQL Server 2005 on Windows.
Thanks,
Vani



Felix Windt 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")
15a5043475dac9278ae75efb4c71f1f6?d=identicon&s=25 Felix Windt (Guest)
on 2007-07-30 14:58
(Received via mailing list)
> Vani
> up the ODBC
> > Server = PRODSK0322
> Posted via http://www.ruby-forum.com/.
>

Try http://support.microsoft.com/kb/305599

That might help creating the DSN you refer to in "DBI:ODBC:DSN".
36eb6a76b5d3c9d71acb38c0a3af1927?d=identicon&s=25 Paul Tozer (ptozer)
on 2007-08-03 17:26
Felix Windt wrote:
>> Vani
>> up the ODBC
>> > Server = PRODSK0322
>> Posted via http://www.ruby-forum.com/.
>>
>
> Try 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
This topic is locked and can not be replied to.