使用Windows身份验证连接SQL服务器的Ruby方法

4

我参考了这个页面:

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

以下是我的代码:

  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  

我正在尝试使用Windows身份验证将Ruby连接到SQL Server。但是我遇到了错误

无法加载驱动程序“ADO”(底层错误:未初始化常量DBI :: DBD :: ADO)

有任何想法吗?

谢谢


这个有帮助吗?http://gordonotspanish.blogspot.com/2010/02/resolved-uninitialized-constant.html - ScottJShea
2个回答

0

你不需要TinyTds,我的系统是:ruby 1.9.3,ruby DevKit,sql server 2012,windows 7

首先安装这些gems;

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

下面的工作脚本连接成功(不确定 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?

如果你直接将SQL Server实例传递给连接字符串,并带有反斜杠,那么会出现错误。但如果将其作为变量传递,则可以接受。

-1

您所参考的页面已经三年历史了。

今天,您应该使用TinyTds来轻松访问MS SqlServer(请在Github上查看)。


1
谢谢,我查看了这两个 Git 仓库 https://github.com/rails-sqlserver/activerecord-sqlserver-adapter 和 https://github.com/rails-sqlserver/tiny_tds ,但仍然没有看到如何使用 Trusted Connection 连接 SQL Server 的示例。您能否提供一个示例呢? - icn
哦,抱歉,TinyTds目前不支持Windows身份验证。 - Klaus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接