Ruby连接使用windows下sql server数据库的方法
来源:爱站网时间:2021-01-05编辑:网友分享
今天爱站小编将为大家介绍Ruby连接使用windows下sql server数据库的方法,是不是很多用户们都非常的好奇呢?下面我们就带着疑问一起去看看吧。
今天爱站小编将为大家介绍Ruby连接使用windows下sql server数据库的方法,是不是很多用户们都非常的好奇呢?下面我们就带着疑问一起去看看吧。
require 'win32ole'
class SqlServer
# This class manages database connection and queries
attr_accessor :connection, :data, :fields
def initialize
@connection = nil
@data = nil
end
def open
# Open ADO connection to the SQL Server database
connection_string = "Provider=SQLOLEDB.1;"
connection_string
测试代码如下:
db = SqlServer.new
db.open
db.query("SELECT PLAYER FROM PLAYERS WHERE TEAM = 'REDS';")
field_names = db.fields
players = db.data
db.close
db = SqlServer.new('localhost', 'sa', 'SOMEPASSWORD')
db.open('Northwind')
db.query("SELECT * from Customers;")
puts field_names = db.fields
cust = db.data
puts cust.size
puts cust[0].inspect
db.close
抄到的别人版本的:
MSSQL
require "dbi"
require "win32ole"
WIN32OLE.codepage = WIN32OLE::CP_UTF8
require 'iconv'
Re_cn=/[\x7f-\xff]/
class MssqlDb
attr_accessor :mdb, :connection, :data, :fields
def initialize(host,mdb,user,pass)
@host= host
@mdb=@database= mdb
@username= user
@password= pass
@connection = nil
@data = nil
@fields = nil
end
def open
connection_string = "Provider=SQLOLEDB.1;User ID=@username;password=@password;Data Source=@host,1433;Initial Catalog=@mdb"
@connection = WIN32OLE.new('ADODB.Connection')
@connection.Open(connection_string)
@password=''
end
def query(sql)
recordset = WIN32OLE.new('ADODB.Recordset')
recordset.Open(sql, @connection)
@fields = []
recordset.Fields.each do |field|
@fields
上一篇:Ruby中的数值类型及常量介绍
下一篇:Ruby如何优化继承类
