require 'win32ole'
class SqlServer
# This class manages database connection and queries
attr_accessor :connection,:data,:fields
attr_writer :username, :password
def initialize(host, username = 'sa', password='')
@connection = nil
@data = nil
@host = host
@username = username
@password = password
end
def open(database)
# Open ADO connection to the SQL Server database
connection_string = "DRIVER=SQL Server;"
connection_string << "SERVER=#{@host};"
connection_string << "UID=#{@username};"
connection_string << "PWD=#{@password};"
connection_string << "APP=Microsoft Office 2003;WSID=user3130.fpihz.com;"
connection_string<<"DATABASE=#{database}"
@connection = WIN32OLE.new('ADODB.Connection')
@connection.Open(connection_string)
end
def query(sql)
# Create an instance of an ADO Recordset
recordset = WIN32OLE.new('ADODB.Recordset')
# Open the recordset, using an SQL statement and the
# existing ADO connection
recordset.Open(sql, @connection)
# Create and populate an array of field names
@fields = []
recordset.Fields.each do |field|
@fields << field.Name
end
begin
# Move to the first record/row, if any exist
recordset.MoveFirst
# Grab all records
@data = recordset.GetRows
rescue
@data = []
end
recordset.Close
# An ADO Recordset's GetRows method returns an array
# of columns, so we'll use the transpose method to
# convert it to an array of rows
@data = @data.transpose
end
def close
@connection.Close
end
end
#let me see
db = SqlServer.new('FPI301\MSSQLSERVER2008', 'sa', '123456')
db.open("sword_shizhan_ifs")
db.query('SELECT * from sword_area;')
puts field_names = db.fields
cust = db.data
puts cust.size
cust.each {|s|
print s,"\n"}
db.close
分享到:
相关推荐
Ruby 连接数据库资源汇总。包括有:Ruby连接Mysql,oracle,postgreSQL,sqlite等包.还有ruby/DBI. 有GEM包,也有源代码包。
Ruby 版数据库连接池, 可以参考。 数据库是postgresql
本人学习总结的ruby on rails 3.1.0数据库类查询方法,比较全了
要连接到Oracle数据库,用户必须安装 Ruby/Oracle 调用接口 (OCI8) 库——一个基于 Ruby/DBI (数据库接口模块)的数据库驱动程序。RubyDBI 提供了一个与数据库无关的、类似于 JDBC 或 ODBC 的接口,来实现 Ruby 和...
本章节将向您讲解如何使用 Ruby 访问数据库。Ruby DBI 模块为 Ruby 脚本提供了类似于 Perl DBI 模块的独立于数据库的接口。 DBI 即 Database independent interface,代表了 Ruby 独立于数据库的接口。DBI 在 Ruby ...
ruby on rails 安装 组件下载 连接mysql mssql等数据库 包含一个完整的一对多数据库例子方案
Ubuntu 11.04安装Ruby on rails 连接MySQL数据库.pdf
ruby对应数据库的必需gems包,包括最新的3个版本的dbi源码和gems安装包
一个简单,灵活且功能强大用于Ruby的SQL数据库访问工具包
ruby连接oracle驱动包,支持32位与64位操作系统 Developing applications with Oracle Database Build Ruby and Ruby on Rails applications using the ruby-oci8 driver or JRuby with the Oracle Enhanced ...
rubywork ruby编程例子 逻辑 IO 数据库rubywork ruby编程例子 逻辑 IO 数据库 rubywork ruby编程例子 逻辑 IO 数据库rubywork ruby编程例子 逻辑 IO 数据库rubywork ruby编程例子 逻辑 IO 数据库
SQLite3 - Ruby绑定SQLite3嵌入式数据库
DataObjects - 企图重写现有的Ruby数据库驱动程序符合一个标准接口
NULL 博文链接:https://like-eagle.iteye.com/blog/1012870
自动与sqlite3数据库做连接 数据的插入,查询,删除、更新操作 数据库建立与表建立 支持关于where语句查询的自动拼接。 支持text的模糊查询 windows 关于qt的数据库操作封装类 以及用例: SQLite,是一款轻型的...
Database Cleaner - 在Ruby中清洗数据库的策略
ruby中操作oracle数据库使用的oci8技术相关的gems包,包括3个版本
巧用ruby配备oracle数据库.pdf
直系同源基因数据库