Linux + mono + Apache access to SQLServer database and Oracle Database

Source: Internet
Author: User
Tags oracleconnection
In the past few days, if I access the SqlServer database and Oracle database through the desktop program and ASP.net on the Linux platform, I find that many articles written on the Internet are incomplete and many are useless redundant settings, I did not talk about the principle. I studied it for several days and confirmed that the following settings can fully meet the requirements. Prerequisites: OS: CentOS5.5A. Install mono2.6 and add one.

In the past few days, if I access the SqlServer database and Oracle database through the desktop program and ASP.net on the Linux platform, I find that many articles written on the Internet are incomplete and many are useless redundant settings, I did not talk about the principle. I studied it for several days and confirmed that the following settings can fully meet the requirements. Prerequisites: OS: CentOS5.5 A. Install mono2.6 and add

In the past few days, if I access the SqlServer database and Oracle database through the desktop program and ASP.net on the Linux platform, I find that many articles written on the Internet are incomplete and many are useless redundant settings, I did not talk about the principle. I studied it for several days and confirmed that the following settings can fully meet the requirements.

Prerequisites: OS: CentOS5.5

A. Install mono2.6

Add a new proxy file and write it to http://ftp.novell.com/pub/mono/download-stable/rhel_5 /. This is the latest mono platform officially provided by mono for CentOS.

Yum -- disablerepo = extras * mono * (Note:/etc/yum. repos. d/CentOS-Base.repo in the [extras] section also has a version of mono, but very old should be Version 1.1, here blocked this source, so only install the latest version of mono)

After the installation is complete, the corresponding httpd settings are automatically modified without manual modification.

B. Install freetds

Yum install freetds

C. Install cancelinstantclient

Download the. ZIP file from the official website and decompress it to/opt/oracle/in dozens of MB /.

0. gadgets for accessing Oracle and SqlServer in Linux

0.1 access SQL Server through command line

After freetds is installed, a tool named tsql is installed. The basic usage is as follows:

# Tsql-S SqlServer Server IP address-p port number-U user name-P Password

After you log in correctly, the following prompt appears:

>

Input

> Select * from table name; press ENTER

> Go; enter

The queried content is displayed.

0.2 Access Oracle through command lines

After InstantClient is installed, download a sqlplus (small) file and decompress it in the same directory. Add this directory to PATH

Run the following command to connect to the remote Oracle database:

# Sqlplus username/password @ Server IP/Instance name

Displayed after successful login

SQL>

Input

SQL> select * from table name; press ENTER

The query result is displayed.

1. Access the SQL Server database by mono in Linux

Call method:

Mono uses classes in the System. Data. SqlClient namespace to complete database operations, which are identical to. net Framework in the call method.

Implementation principle:

The implementation of the class library in System. Data. SqlClient relies on calling the function of the freetds shared library through P/Invoke. Therefore, you must install freetds by using yum install freetds in CentOS5.5.

Sample Code:

Sqlserver. cs

Using System; using System. text; using System. data. sqlClient; namespace ConsoleApplication1 {class Program {static void Main (string [] args) {string connStr = "Data Source = Database Server IP address; Initial Catalog = database name; user ID = database username; Password = Database Password "; SqlConnection conn = new SqlConnection (connStr); SqlCommand cmd = new SqlCommand (" query SQL statement ", conn ); sqlDataReader reader = null; try {conn. open (); reader = cmd. executeRe Ader (); while (reader. read () {Console. writeLine (reader. getValue (1 ). toString () ;}} catch (Exception e) {Console. writeLine (e. message);} finally {if (reader! = Null) {reader. Close () ;}else Console. WriteLine ("reader = null"); conn. Close ();}}}}

Compile command: gmcs-r: System. Data sqlserver. cs

Run the command: mono sqlserver.exe

2. mono Access Oracle databases in Linux

Call method:

Mono uses classes in the System. Data. OracleClient namespace to perform database operations, which are identical to. net Framework in the call method.

It should be noted that at present, Microsoft does not recommend that.. net platform with System. data. oracle provides an Oracle. data. access class library to provide.. net Access to the Oracle database. However, on the mono platform, System. Data. OracleClient is still the best way to access the Oracle database.

Implementation principle:

The implementation of the class library in System. Data. OracleClient relies on calling the libclntsh. so shared library function through P/Invoke. Install the InstantClient provided by Oracle. This software is a typical green software. Download it and decompress it. It should be noted that mono can find this shared library, so it is necessary to add its path to the LD_LIBRARY_PATH environment variable. For example, if the directory is/opt/oracle/instantclient_11_2 after decompression, write the following in/etc/profile:

Export LD_LIBRARY_PATH =/opt/oracle/intantclient_11_2: $ LD_LIBRARY_PATH. You also need to set NLS_LANG as follows:

Export NLS_LANG = AMERICAN_AMERICA.UTF8

Finally, make a soft connection.

Ln-s/opt/oracle/instantclient_11_2/libclntsh. so.0.0.0/opt/oracle/intantclient_11_2/libclntsh. so

Then execute ldconfig to refresh the cache.

Now mono can access the oracle database.

Sample Code:

Oracle. cs

Using System; using System. text; using System. data. oracleClient; namespace ConsoleApplication1 {class Program {static void Main (string [] args) {string connStr = "User ID = User name;" + "Password = Password; "+" Data Source = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (HOST = ORCLE Server IP) (PORT = 1521 ))) (CONNECT_DATA = (SID = Instance name) (SERVER = DEDICATED) "; OracleConnection conn = new OracleConnection (connStr); OracleCommand Cmd = new OracleCommand ("select * from/" table name/"", conn); OracleDataReader reader = null; try {conn. open (); reader = cmd. executeReader (); while (reader. read () {Console. writeLine (reader. getValue (1 ). toString () ;}} catch (Exception e) {Console. writeLine (e. message);} finally {if (reader! = Null) {reader. Close () ;}else Console. WriteLine ("reader = null"); conn. Close ();}}}}

Compilation command: gmcs-r: System. Data. OracleClient oracle. cs

Run the command: mono oracle.exe

3. Apache-based mono access to SqlServer

In CentOS, if freetds, mono, and mod_mono installed through yum, there is no problem, which is basically the same as 1. No more details.

4. Apache-based mono access to Oracle

Note that environment variables such as LD_LIBRARY_PATH and NLS_LANG set in/etc/profile are not informed in httpd. httpd is a service program and is not started from shell. At this time,/opt/novell/mono/bin/mod-mono-server is also started through httpd, so these environment variables are unknown, as a result, mono cannot determine the libclntsh that needs to be called through p/Invoke. so, it will cause access to Oracle. solution:

Modify/opt/novell/mono/bin/mod-mono-server as follows:

#! /Bin/sh

Export LD_LIBRARY_PATH =/opt/oracle/instantclient_11_2: $ LD_LIBRARAY_PATH

Export NLS_LANG = AMERICAN_AMERICA.UTF8

Exec/opt/novell/mono/bin/mono $ ............................ .....

Restart the httpd service.

If you need to access Oracle and SqlServer under JDBC, refer to my other blog: http://blog.csdn.net/smstong/article/details/8129536

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.