Ruby串連SQL Server資料庫配置教程

來源:互聯網
上載者:User

因為工作需要,要分析存放在SQL Server上的資料,所以不得不研究一下如何使用Ruby訪問SQL Server,發現其實還是很簡單的:

安裝FreeTDS

1.下載FreeTDS原始碼
2.解壓編譯安裝:

 代碼如下 複製代碼

./configure --prefix=/usr/local/freetds && make && sudo make install

安裝Tiny_TDS


Tiny_TDS,安裝和使用非常簡單,推薦使用:

 代碼如下 複製代碼
sudo gem install tiny_tds -- --with-freetds-dir=/usr/local/freetds

用tiny_tds訪問SQL Server很簡單:

 代碼如下 複製代碼

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

在ActiveRecord上使用Tiny_TDS


這也非常簡單,參考這個教程Using TinyTDS:

 代碼如下 複製代碼
gem install activerecord-sqlserver-adapter

配置database.yml如下:

 代碼如下 複製代碼

development:
    adapter: sqlserver
    host: mydb.net
    database: myapp_development
    username: sa
    password: secret 

小提示,如果你覺得配置麻煩可以使用微軟官方有ODBC驅動,地址如下:http://www.microsoft.com/en-us/download/details.aspx?id=28160
自己去研究一下吧。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.