脚本专栏 
首页 > 脚本专栏 > 浏览文章

Ruby连接使用windows下sql server数据库代码实例

(编辑:jimmy 日期: 2024/11/26 浏览:3 次 )
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 


上一篇:详解Lua中的表的概念及其相关操作方法
下一篇:Ruby优化继承类实例
一句话新闻
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 网站地图 SiteMap