Code for connecting to SQL Server?

Discussion in 'Ruby' started by Anukul Singhal, May 7, 2008.

  1. Hi,

    I want to connect to my SQL Server DB through ruby code.

    Searched on the connection and was provided some steps like:

    1. Save the latest source distribution of ruby-dbi and copy the file
    ADO.rb to
    X:/ruby/lib/ruby/site_ruby/1.8/DBD/ADO/ADO.rb

    2. Then, in your database.yml,

    development:
    adapter: sqlserver
    database: database_name
    host: server_name
    username: user_name
    password: pwd

    I am not able to find this file.

    Can anyone tell me for invoking sql server in SCITE editor?

    Is there any other procedure 'coz I dont have rails installed?

    Thanks,
    Anukul
    --
    Posted via http://www.ruby-forum.com/.
    Anukul Singhal, May 7, 2008
    #1
    1. Advertising

  2. [Note: parts of this message were removed to make it a legal post.]

    Hey,
    Do you want to invoke the sql using ruby code or ruby on rails....
    If its just ruby then you u can go a head like this:

    require 'mysql'
    mysql = Mysql.init()
    mysql.connect('localhost','USERNAME','PASSWORD')
    mysql.select_db(' DATABASE NAME')
    and proceed with the rest of the operations as required .

    If its in Ruby on rails then you can configure your database.yml file

    cheers,
    krantih






    On Wed, May 7, 2008 at 4:48 PM, Anukul Singhal <>
    wrote:

    > Hi,
    >
    > I want to connect to my SQL Server DB through ruby code.
    >
    > Searched on the connection and was provided some steps like:
    >
    > 1. Save the latest source distribution of ruby-dbi and copy the file
    > ADO.rb to
    > X:/ruby/lib/ruby/site_ruby/1.8/DBD/ADO/ADO.rb
    >
    > 2. Then, in your database.yml,
    >
    > development:
    > adapter: sqlserver
    > database: database_name
    > host: server_name
    > username: user_name
    > password: pwd
    >
    > I am not able to find this file.
    >
    > Can anyone tell me for invoking sql server in SCITE editor?
    >
    > Is there any other procedure 'coz I dont have rails installed?
    >
    > Thanks,
    > Anukul
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    kranthi reddy, May 7, 2008
    #2
    1. Advertising

  3. kranthi reddy wrote:
    > Hey,
    > Do you want to invoke the sql using ruby code or ruby on rails....
    > If its just ruby then you u can go a head like this:
    >
    > require 'mysql'
    > mysql = Mysql.init()
    > mysql.connect('localhost','USERNAME','PASSWORD')
    > mysql.select_db(' DATABASE NAME')
    > and proceed with the rest of the operations as required .
    >
    > If its in Ruby on rails then you can configure your database.yml file
    >
    > cheers,
    > krantih
    >
    >
    >
    >
    >
    >
    > On Wed, May 7, 2008 at 4:48 PM, Anukul Singhal
    > <>


    Hi,

    Thanks for the info.

    My basic aim is to connect to SQL Server 2005 through Ruby without using
    rails.

    Can you help in providing a code snippet for connecting to it?

    Thanks,
    Anukul
    --
    Posted via http://www.ruby-forum.com/.
    Anukul Singhal, May 7, 2008
    #3
  4. Hi Anukul,

    Try this. I am working windows here.

    TRY.rb

    ***************************************************************
    =begin
    OS : Windows here
    Ruby : 1.8.6
    DB :Mysql
    Gem :> gem install mysql
    =end

    require "mysql"
    #require mysql gem
    connection = Mysql.connect("localhost", "root", "", "cdcol")
    #localhost = hostname
    #root = username of mysql DB
    #""=password of mysql DB
    #cdcol = DB name
    output = connection.query("SELECT * FROM cds;")
    #cds = table name
    output.each do |x|
    puts x.inspect
    end

    ***************************************************************

    Regards,
    P.Raveendran
    Railsfactory
    http://raveendran.wordpress.com
    --
    Posted via http://www.ruby-forum.com/.
    Raveendran Jazzez, May 7, 2008
    #4
  5. [Note: parts of this message were removed to make it a legal post.]

    The code what I wrote above helps u to connect to the sql server by modifing
    it a lil
    require 'mysql'
    mysql = Mysql.init()
    mysql.connect('localhost','USERNAME','PASSWORD')
    mysql.select_db(' DATABASE NAME')
    result = []
    result= mysql.query("SELECT name FROM users;")
    result.each do |x|
    puts x.name
    end

    from the above when u run ur ruby file u can see the result as list of users
    in the data base table called users .
    name is the column in the users table.
    hopefully this should work
    You can alwyas change the database type from mysql/postgresql/sqlite3/sql by
    modifing in the correct place form the above code
    cheers,
    kranthi.

    On Wed, May 7, 2008 at 5:53 PM, Raveendran Jazzez <>
    wrote:

    > Hi Anukul,
    >
    > Try this. I am working windows here.
    >
    > TRY.rb
    >
    > ***************************************************************
    > =begin
    > OS : Windows here
    > Ruby : 1.8.6
    > DB :Mysql
    > Gem :> gem install mysql
    > =end
    >
    > require "mysql"
    > #require mysql gem
    > connection = Mysql.connect("localhost", "root", "", "cdcol")
    > #localhost = hostname
    > #root = username of mysql DB
    > #""=password of mysql DB
    > #cdcol = DB name
    > output = connection.query("SELECT * FROM cds;")
    > #cds = table name
    > output.each do |x|
    > puts x.inspect
    > end
    >
    > ***************************************************************
    >
    > Regards,
    > P.Raveendran
    > Railsfactory
    > http://raveendran.wordpress.com
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    kranthi reddy, May 7, 2008
    #5
  6. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Anukul Singhal wrote:

    | Hi,
    |
    | Thanks for the info.
    |
    | My basic aim is to connect to SQL Server 2005 through Ruby without using
    | rails.

    You could use ActiveRecord. It is seperate from Rails, too.

    | Can you help in providing a code snippet for connecting to it?

    http://ar.rubyonrails.com/classes/ActiveRecord/ConnectionAdapters/SQLServerAdapter.html

    - --
    Phillip Gawlowski
    Twitter: twitter.com/cynicalryan
    Blog: http://justarubyist.blogspot.com

    Use debugging compilers.
    ~ - The Elements of Programming Style (Kernighan & Plaugher)
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.8 (MingW32)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iEYEARECAAYFAkghodAACgkQbtAgaoJTgL/dSACfb41YflRqaS2kXjJL8qH3TgI4
    d9AAn18iSM4B7jSVhxHOhO9NqZqBFAAR
    =IFw5
    -----END PGP SIGNATURE-----
    Phillip Gawlowski, May 7, 2008
    #6
  7. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    kranthi reddy wrote:
    | The code what I wrote above helps u to connect to the sql server by
    modifing
    | it a lil
    | require 'mysql'
    | mysql = Mysql.init()
    | mysql.connect('localhost','USERNAME','PASSWORD')
    | mysql.select_db(' DATABASE NAME')
    | result = []
    | result= mysql.query("SELECT name FROM users;")
    | result.each do |x|
    | puts x.name
    | end

    MySQL != MSSQL

    MSSQL: en.wikipedia.org/wiki/Microsoft_SQL_Server
    MySQL: en.wikipedia.org/wiki/MySQL

    - --
    Phillip Gawlowski
    Twitter: twitter.com/cynicalryan
    Blog: http://justarubyist.blogspot.com

    ~ I'm looking for something that can deliver a 50-pound payload of snow
    ~ on a small feminine target. Can you suggest something? Hello...?
    ~ --- Calvin
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.8 (MingW32)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iEYEARECAAYFAkgho+sACgkQbtAgaoJTgL9qxwCcCe6QA31tSUG1bKya+CMA+efT
    W8UAn0jVy3Dhjn8L4KJDHrrHW1TysD0h
    =m/XE
    -----END PGP SIGNATURE-----
    Phillip Gawlowski, May 7, 2008
    #7
  8. Anukul Singhal

    Guest

    On May 7, 6:18 am, Anukul Singhal <> wrote:
    > Hi,
    >
    > I want to connect to my SQL Server DB through ruby code.
    >
    > Searched on the connection and was provided some steps like:
    >
    > 1. Save the latest source distribution of ruby-dbi and copy the file
    > ADO.rb to
    > X:/ruby/lib/ruby/site_ruby/1.8/DBD/ADO/ADO.rb
    >
    > 2. Then, in your database.yml,
    >
    > development:
    >    adapter: sqlserver
    >    database: database_name
    >    host: server_name
    >    username: user_name
    >    password: pwd
    >
    > I am not able to find this file.
    >
    > Can anyone tell me for invoking sql server in SCITE editor?
    >
    > Is there any other procedure 'coz I dont have rails installed?
    >
    > Thanks,
    > Anukul
    > --
    > Posted viahttp://www.ruby-forum.com/.


    require 'dbi'

    @server_name='localhost'
    @dbh=DBI.connect("DBI:ADO:provider=SQLNCLI;Data
    Source=#{@server_name};Integrated Security=SSPI")

    if @dbh
    puts "connected"
    else
    puts "not connected"
    end

    @dbh.select_all('SELECT name FROM master.sys.databases ORDER BY 1') do
    | row|
    puts row
    end

    Here's an example.

    Luis
    , May 8, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aaron Bertrand - MVP

    Connecting to SQL Server DB

    Aaron Bertrand - MVP, Dec 3, 2003, in forum: ASP .Net
    Replies:
    14
    Views:
    748
    len nelson
    Oct 8, 2004
  2. Robert
    Replies:
    4
    Views:
    2,218
    George Ter-Saakov
    Feb 9, 2004
  3. Peri
    Replies:
    1
    Views:
    394
    Patrick Olurotimi Ige
    Jun 13, 2005
  4. Daves
    Replies:
    1
    Views:
    363
  5. =?Utf-8?B?TmVpbCBQYWRkb2Nr?=

    Connecting to SQL 2000 database with SQL 2005 tools installed

    =?Utf-8?B?TmVpbCBQYWRkb2Nr?=, May 2, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    396
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    May 2, 2007
Loading...

Share This Page