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 Öе÷ÓÃJmail×é¼þ·¢ËÍÓʼþ
Ô¤±¸ÖªÊ¶
1£®OLE×Ô¶¯»¯º¯Êý
OLE×Ô¶¯»¯Ê¹Ó¦ÓóÌÐòÄܹ»¶ÔÁíÒ»¸öÓ¦ÓóÌÐòÖÐʵÏֵĶÔÏó½øÐвÙ×÷£¬»òÕß½«¶ÔÏ󹫿ªÒÔ±ã¿ÉÒÔ¶ÔÆä½øÐвÙ×÷¡£×Ô¶¯»¯¿Í»§¶ËÊǿɶÔÊôÓÚÁíÒ»¸öÓ¦ÓóÌÐòµÄ¹«¿ª¶ÔÏó½øÐвÙ×÷µÄÓ¦ÓóÌÐò£¬±¾ÎÄÖµµÃÊÇSql Server¡£¹«¿ ......
sql server 2005Ö»±¸·ÝÊý¾Ý¿âµÄ½á¹¹
1¡¢´ò¿ªsql server enterpirise management studio£¬ÓÃsaÕÊ»§Á¬½Óµ½Êý¾Ý¿â·þÎñÆ÷¡£
2¡¢Ñ¡ÔñÐèÒª±¸·ÝµÄÊý¾Ý¿â£¬ÓÒ»÷Êó±êÔÚµ¯³öµÄ²Ëµ¥ÖÐÑ¡Ôñ“ÈÎÎñ”——“Éú³É½Å±¾”£¬°´Õսű¾Ïòµ¼£¬ÏÂÒ»²½¾Í¿ÉÒÔÁË¡£
ÕâÑù»á½«´ËÊý¾Ý¿âÖеÄËùÓжÔÏóµÄ´´½¨³ÉÉúÒ»¸ö½Å±¾Î ......
SQLSERVER 2005²ÉÓÃ'SQLSERVERÉí·ÝÑéÖ¤'È¥µÇ¼, ³ö´íµÄÔÒòÊÇ:'Óû§ 'sa' µÇ¼ʧ°Ü¡£¸ÃÓû§Óë¿ÉÐÅ SQL Server Á¬½ÓÎÞ¹ØÁª'.½ñÌìÉÏÍøÉϲéÁË°ëÌì²Å¸ã¶¨µÄ¡£
¾ßÌåµÄ·½·¨ÊÇ£º ¡¡¡¡
1£º´ò¿ªSQL Server Manager¹ÜÀíÆ÷£¡ÔÚ×óÃæÕÒµ½ ‘°²È«ÐÔ’ µ¥»÷ÓÒ¼ü Ñ¡Ôñ‘н¨”£¬“µÇ¼” µ¯³öÒ»¸ö¶Ô»°¿ò£¬Ô ......
Èç¹ûÄãÕýÔÚ¸ºÔðÒ»¸ö»ùÓÚSQL ServerµÄÏîÄ¿£¬»òÕßÄã¸Õ¸Õ½Ó´¥SQL Server£¬Ä㶼ÓпÉÄÜÒªÃæÁÙһЩÊý¾Ý¿âÐÔÄܵÄÎÊÌ⣬ÕâƪÎÄÕ»áΪÄãÌṩһЩÓÐÓõÄÖ¸µ¼£¨ÆäÖдó¶àÊýÒ²¿ÉÒÔÓÃÓÚÆäËüµÄDBMS£©¡£
ÔÚÕâÀÎÒ²»´òËã½éÉÜʹÓÃSQL ServerµÄÇÏÃÅ£¬Ò²²»ÄÜÌṩһ¸ö°üÖΰٲ¡µÄ·½°¸£¬ÎÒËù×öµÄÊÇ×ܽáһЩ¾Ñé----¹ØÓÚÈçºÎÐγÉÒ»¸öºÃµÄÉè¼Æ¡£Õ ......
Ò»¡¢sqlÊéд¹æ·¶£º
1¡¢sqlÓï¾äµÄËùÓбíÃû¡¢×Ö¶ÎÃûÈ«²¿Ð¡Ð´£¬ÏµÍ³±£Áô×Ö¡¢ÄÚÖú¯ÊýÃû¡¢sql±£Áô×Ö´óд¡£
2¡¢Á¬½Ó·ûor¡¢in¡¢and¡¢ÒÔ¼°£½¡¢<=¡¢>=µÈÇ°ºó¼ÓÉÏÒ»¸ö¿Õ¸ñ¡£
3¡¢¶Ô½ÏΪ¸´ÔÓµÄsqlÓï¾ä¼ÓÉÏ×¢ÊÍ£¬ËµÃ÷Ëã·¨¡¢¹¦ÄÜ¡£
&nb ......