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 << "Persist Security Info=False;"
    connection_string << "User ID=USER_ID;"
    connection_string << "password=PASSWORD;"
    connection_string << "Initial Catalog=DATABASE;"
    connection_string << "Data Source=IP_ADDRESS;"
    connection_string << "Network Library=dbmssocn"
    @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

测试代码如下:

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 << field.Name 
   end 
   begin 
    @data = recordset.GetRows.transpose 
   rescue 
    @data = [] 
   end 
   recordset.Close 
  end 
  
  def queryGB(sql) 
   if sql=~ Re_cn 
   sql = utf8_to_gb(sql) 
   end 
   recordset = WIN32OLE.new('ADODB.Recordset') 
   recordset.Open(sql, @connection) 
   @fields = [] 
   recordset.Fields.each do |field| 
    @fields << field.Name 
   end 
   begin 
    @data = recordset.GetRows.transpose 
   rescue 
    @data = [] 
   end 
   recordset.Close 
  end 
  
  def execute(sql) 
   @connection.Execute(sql) 
  end 
  
  def executeGB(sql) 
   if sql=~ Re_cn 
   sql = utf8_to_gb(sql) 
   end 
   @connection.Execute(sql) 
  end 
  
  def close 
   @connection.Close 
  end 
   
  def utf8_to_gb(s) 
   p 'conv to gb18030' 
   Iconv.conv("GB18030//IGNORE","UTF-8//IGNORE",s) 
  end 
  def gb_to_utf8(s) 
   p 'conv to utf8' 
   Iconv.conv("UTF-8//IGNORE","GB18030//IGNORE",s) 
  end  
 end 
  
  
  
  
  
  
 ACCESS 
 require "win32ole" 
 class AccessDb 
   attr_accessor :mdb, :connection, :data, :fields 
  
   def initialize(mdb=nil) 
     @mdb = mdb 
     @connection = nil 
     @data = nil 
     @fields = nil 
   end 
  
   def open 
     connection_string = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' 
     connection_string << @mdb 
     @connection = WIN32OLE.new('ADODB.Connection') 
     @connection.Open(connection_string) 
         p 'access open ok.' 
   end 
  
   def query(sql) 
     recordset = WIN32OLE.new('ADODB.Recordset') 
     recordset.Open(sql, @connection) 
     @fields = [] 
     recordset.Fields.each do |field| 
       @fields << field.Name 
     end 
     begin 
       @data = recordset.GetRows.transpose 
     rescue 
       @data = [] 
     end 
     recordset.Close 
   end 
  
   def execute(sql) 
     @connection.Execute(sql) 
   end 
  
   def close 
     @connection.Close 
   end 
 end 


广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。