时间:2021-05-22
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 endend
测试代码如下:
db = SqlServer.newdb.opendb.query("SELECT PLAYER FROM PLAYERS WHERE TEAM = 'REDS';")field_names = db.fieldsplayers = db.datadb.closedb = 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声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
ViewCode复制代码代码如下:访问数据库的通用代码实例1.请输入相应数据库连接字符串2.请输入相应SQL查询命令语句3.请选择所连接的数据库类型Access
易语言连接数据库的方法如下: 一、使用外部数据库控件 代码为: 1、如果真(外部数据库1.打开SQL数据库(服务器IP,用户名,密码,数据库名称,,)=假
PowerDesigner连接数据库的实例详解PowerDesigner连接数据库大致如下列图所示:选择需要连接的数据库类型:有两个选择,一、导入Sql脚本文件
本文实例讲述了php使用pdo连接并查询sql数据库的方法。分享给大家供大家参考。具体实现代码如下:复制代码代码如下:$login="root";$passwd
获取数据连接后,即可对数据库中的数据进行修改和查看。使用Statement接口可以对数据库中的数据进行修改,下面是程序演示。/***获取数据库连接,并使用SQL