Ruby 连接 Sql Server

因为工作需要,要分析存放在SQL Server上的数据,所以不得不研究一下如何使用Ruby访问SQL Server,发现其实还是很简单的:

== 安装FreeTDS ==


  • 安装预要求的包:

ruby aptitude install freetds-dev

  • 3.安装Tiny_TDS Tiny_TDS,安装和使用非常简单,推荐使用:

ruby sudo gem install tiny_tds

用[https://github.com/rails-sqlserver/tiny_tds]访问SQL Server很简单:

1
2
3
4
5
6
7
  require 'tiny_tds'
  client = TinyTds::Client.new(:username => 'fankai', :password => 'fankai', :host => '192.168.0.1', :database => 'test')
  result = client.execute("select top 10 * from User");
  result.each do |row|
    puts row
  end

1
gem install activerecord-sqlserver-adapter

配置database.yml如下:

1
2
3
4
5
6
  development:
    adapter: sqlserver
    host: mydb.net
    database: myapp_development
    username: sa
    password: secret

原文见[http://robbinfan.com/blog/44/ruby-connect-sqlserver]

Comments