ruby Á¬½Ó²Ù×÷ sql2005
The following is improved version of the code created by David Mullet, from
http://rubyonwindows.blogspot.com/2007/03/ruby-ado-and-sqlserver.html
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 = "Provider=SQLOLEDB.1;"
connection_string << "Persist Security Info=False;"
connection_string << "User ID=#{@username};"
connection_string << "password=#{@password};"
connection_string << "Initial Catalog=#{database};"
connection_string << "Data Source=#{@host};"
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 = @dat
Ïà¹ØÎĵµ£º
SQL Server 2005 ¾µÏñ¹¹½¨ÊÖ²á
Ò»¡¢ ¾µÏñ¼ò½é
1¡¢ ¼ò½é
Êý¾Ý¿â¾µÏñÊǽ«Êý¾Ý¿âÊÂÎñ´¦Àí´ÓÒ»¸öSQL ServerÊý¾Ý¿âÒÆ¶¯µ½²»Í¬SQL Server»·¾³ÖеÄÁíÒ»¸öSQL ServerÊý¾Ý¿âÖС£¾µÏñ²»ÄÜÖ±½Ó·ÃÎÊ;ËüÖ»ÓÃÔÚ´íÎó»Ö¸´µÄÇé¿öϲſÉÒÔ±»·ÃÎÊ¡£
Òª½øÐÐÊý¾Ý¿â¾µÏñËùÐèµÄ×îСÐèÇó°üÀ¨ÁËÁ½¸ö²»Í¬µÄSQL ServerÔËÐл·¾³¡£Ö÷·þÎñÆ÷± ......
SQL Server 2005 ¾µÏñ¹¹½¨ÊÖ²á(2)
3¡¢ ½¨Á¢¾µÏñ
ÓÉÓÚÊÇʵÑ飬ûÓÐΪ·þÎñÆ÷ÅäÖÃË«Íø¿¨£¬IPµØÖ·ÓëͼÓе㲻һÑù£¬µ«ÊÇÔÀíÒ»Ñù¡£
--Ö÷»úÖ´ÐУº
1ALTER DATABASE shishan SET PARTNER = 'TCP://10.168.6.45:5022';
--Èç¹ûÖ÷ÌåÖ´Ðв»³É¹¦£¬³¢ÊÔÔÚ±¸»úÖÐÖ´ÐÐÈçÏÂÓï¾ä£º
1ALTER DATABASE ......
Ò»¡¢»ù´¡
1¡¢ËµÃ÷£º´´½¨Êý¾Ý¿â
Create DATABASE database-name
2¡¢ËµÃ÷£ºÉ¾³ýÊý¾Ý¿â
drop database dbname
3¡¢ËµÃ÷£º±¸·Ýsql server
--- ´´½¨ ±¸·ÝÊý¾ÝµÄ device
USE master
EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind ......
/*
½¨±í£º
dept:
deptno(primary key),dname,loc
emp:
empno(primary key),ename,job,mgr,sal,deptno
*/
1 Áгöemp±íÖи÷²¿ÃŵIJ¿Ãźţ¬×î¸ß¹¤×Ê£¬×îµÍ¹¤×Ê
select max(sal) as ×î¸ß¹¤×Ê,min(sal) as ×îµÍ¹¤×Ê,deptno from emp group by deptno;
2 Áгöemp±íÖи÷²¿ÃÅjobΪ'CLERK'µÄÔ±¹¤µÄ×îµÍ¹¤×Ê£¬×î¸ß¹¤×Ê
sele ......