Ruby sqlserver connection method

 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"  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 

 You can then use this class as follows:  
 
 db = SqlServer.new
 db.open
 db.query ( "SELECT PLAYER FROM PLAYERS WHERE TEAM = 'REDS';")
 field_names = db.fields
 players = db.data
 Db.Close

The above code is, of course, incomplete and can certainly be improved and extended (error handling, etc.). But, hopefully, it provides you with a solid foundation on which to build.

UPDATE: You might like to know that you can automate many of your SQL Server administrative tasks by leveraging Distributed Management Objects (SQL-DMO). I've explained this in a later article here .

Thanks for stopping by!

分类:Ruby 时间:2009-03-10 人气:1150
分享到:
blog comments powered by Disqus

相关文章

  • SQL server data source connection 2010-05-10

    model: package model; public class UserInfo { private int id; private String userName; private String address; public UserInfo(String userName, String address) { super(); this.userName = userName; this.address = address; } public UserInfo(int id, Str

  • IV. Tomcat5 to configure the connection pool and data source 2010-10-06

    1, DataSource interface, introduced (1) DataSource Overview JDBC1.0 they are used DriverManager class to create a data source. JDBC2.0 use an alternative approach, using the DataSource implementation, the code becomes more compact, more easily contro

  • Intellectual platform for the two data sets the data source connection configuration 2011-08-26

    In the set of intellectual data platform provides two types of data source connection is configured, all through reportConfig.xml profiles, specific methods are as follows: 1.jndi data source configuration (ie: configure the dataSource) This configur

  • spring hibernate many data source configuration collection 2009-04-09

    Spring2.0.1 later many have supported the configuration data source, and can be run at a time when dynamic loading of different data sources. Through inheritance AbstractRoutingDataSource on many data sources can achieve the dynamic conversion. Does

  • many spring hibernate collection of the data source configuration 2009-04-09

    Spring2.0.1 later many have supported the configuration data source, and can be run at a time when dynamic loading of different data sources. Through inheritance AbstractRoutingDataSource on many data sources can achieve the dynamic conversion. Does

  • Configure Spring Data Source 2010-04-04

    No matter what kind of lasting through technology, we must access the database through the data connection, in the Spring, the data connection is obtained through the data source. In the past, applications, the data source is usually provided by the

  • Configure a data source, spring 2010-03-26

    Configure a data source Spring in the third-party dependency package contains two data source implementation class package, One is Apache's DBCP, second, C3P0. In the Spring configuration file can take advantage of this configuration of either a data

  • Tomcat.Apache. Servlet.xml. JNDI (data source) 2010-10-12

    Transfer from: http://www.newasp.net/tech/java/14525.html 1, the installation TOMCAT latest version is 5.0.29 (http://jakarta.apache.org/site/binindex.cgi) WINDOWS if it can automatically find you under the JDK or set JAVA_HOME = c: / jdk Under the L

  • jndi + rmi + proxool application load remote data source 1 (original) 2010-04-27

    Reply: The recent account recharge system to do online games, working encountered a problem. Since pv high 10 servers to do with the application of load, the load is simple poll (guess that did not ask the system, huh, huh). Applications and database

  • Memorandum of JDBC data source of knowledge 2010-12-23

    1.JDBC two ways to create CONNECTION By Class.forName () loads the database driver, and then use the DriverManager.getConnection () to get connected. By javax.sql.DataSource provides another way to establish a database connection. JNDI server through

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 闽ICP备15018612号

processed in 0.035 (s). 13 q(s)