Configuration instance for accessing the SQL Server database in Ruby, rubysql

Source: Internet
Author: User

Configuration instance for accessing the SQL Server database in Ruby, rubysql

To analyze the data stored on SQL Server, you have to study how to use Ruby to access SQL Server. In fact, it is quite simple:

Install FreeTDS

Download FreeTDS source code

Decompress, compile, and install:
Copy codeThe Code is as follows:
./Configure -- prefix =/usr/local/freetds & make & sudo make install

Install Tiny_TDS

Tiny_TDS is easy to install and use. It is recommended:
Copy codeThe Code is as follows:
Sudo gem install tiny_tds -- with-freetds-dir =/usr/local/freetds

Using tiny_tds to access SQL Server is simple:
Copy codeThe Code is as follows:
Require 'Tiny _ tds'
Client = TinyTds: Client. new (: username => 'fanai',: password => 'fanai',: host => '2017. 168.0.1 ',: database => 'test ')
Result = client.exe cute ("select top 10 * from User ");
Result. each do | row |
Puts row
End

Use Tiny_TDS on ActiveRecord

This is also very simple. Refer to this tutorial Using TinyTDS:
Copy codeThe Code is as follows:
Gem install activerecord-sqlserver-adapter

Configure database. yml as follows:
Copy codeThe Code is as follows:
Development:
Adapter: sqlserver
Host: mydb.net
Database: myapp_development
Username: sa
Password: secret


How many methods does ruby connect to the database? How can I connect to SQLServer?

For example, how does ruby connect to SQL SERVER in windows and linux. Yes
 
JAVA programming to access the SQL Server database

Connect to SQL Server using java
(Currently, the most common database for enterprises is SQL Server2000. Therefore, I only want to talk about this. If you have any problems with access, mysql, and Oracle databases, contact me)
There are two methods for connecting to a database using java: jdbc/odbc bridge (data source needs to be configured) and jdbc driver. the latter is strongly recommended here, because the former involves the problem of jdbc to odbc conversion, the execution efficiency is very low.
Download the jdbc driver package (search by search engine). After installation, open the folder and you will see Three jar packages, you only need to set these three jar packages to the environment variable (classpath ).
If the driver is installed in the d: \ sqldriver directory, add d: \ sqldriver \ msbase to classpath. jar; d: \ sqldriver \ mssqlserver. jar; d: \ sqldriver \ msutil. jar;
Note: If you are using windows XP sp2, You need to patch SQL Server with sp3a or sp4.
Test code (the database that comes with SQL Server can be compiled and run directly)
Import java. SQL .*;
Public class SqlTesting {
Public static void main (String args []) {
String url = "jdbc: microsoft: sqlserver: // localhost: 1433; DatabaseName = Northwind ";
String user = "sa ";
String password = "sa ";
String sqlStr = "select CustomerID, CompanyName, ContactName from Customers ";

Try {
Class. forName ("com. microsoft. jdbc. sqlserver. SQLServerDriver ");
System. out. println ("");

Connection con = DriverManager. getConnection (url, user, password );
Statement st = con. createStatement ();
ResultSet rs = st.exe cuteQuery (sqlStr );

While (rs. next ()){
System. out. print (rs. getString ("CustomerID") + "");
System. out. print (rs. getString ("CompanyName") + "");
System. out. println (rs. getString (& quot ...... full text>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.