简体   繁体   中英

ruby connect SQL server by using Windows authentication

I referred this page

http://www.codecapers.com/post/using-ruby-with-sql-server.aspx

my code

  DBI.connect('DBI:ADO:Provider=SQLNCLI;Data Source=SQLSERVER001;Integrated Security=True;Initial Catalog=DB001') do | dbh |
     # Replace mytable with the name of a table in your database.
     dbh.select_all('select top 1000 * from history where type="35"  ') do | row |
        puts row
     end
  end  

and trying to connect ruby with sql server by Windows authentication . But I got error

Unable to load driver 'ADO' (underlying error: uninitialized constant DBI::DBD::ADO)

Any idea?

Thanks

you don't need TinyTds, my system: ruby 1.9.3, ruby DevKit , sql server 2012, windows 7

install these gems first;

gem install dbi
gem install dbd-odbc
gem install activerecord-sqlserver-adapter

working script below connects, (not sure about the require 'pp')

require 'rubygems'
require 'DBI'
require 'pp'

server = 'XXXXX-LT0XXXX\XXX'

database = 'mydatabase'

conn = DBI.connect("DBI:ODBC:DRIVER={SQL Server};Server=#{server};Database=#{database};Trusted_Connection=yes") #==> sets up the connection

puts conn.connected?

if you pass the sql server instance directly into the connection string with a backslash it error but if passed as a variable it accepts

The page you are refering to is three years old.

Today you should use TinyTds for an easy way accessing A MS SqlServer ( look at Github for it )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM