When Oracle is not installed on the computer, C # Calls oracle database and Oracle customer tools,

Source: Internet
Author: User
Tags oracleconnection

When Oracle is not installed on the computer, C # Calls oracle database and Oracle customer tools,

The installation packages of Oracle are usually relatively large and time-consuming. If errors occur during the installation process, the installation may be quite annoying. At work, oracle can be installed on the server. On our local computer, we only need to install an oracle client, and then connect to the server.

For lightweight Oracle clients, I recommend using Navicat For Oracle, which is only 17 MB.

1. download the following dll, I am using oracle11g, these DLL: http://pan.baidu.com/s/1kU1JafX

2. Copy it to the project and set the dll attribute to "Copy to output directory" to "always copy"

3. Add Oracle. DataAccess. dll references to the project.

4. Create oracle operation ORacleDBHelp

/* ===================================================== ========================================================== * Function description: ORacleDBHelp * Creator: Zouqj * creation date: 14:30:22 ===================================================== ========================================================== */using System; using System. collections. generic; using System. text; using System. configuration; using System. data; using System. threading; using System. transactions; using Oracle. dataAcces S. client; using Oracle. dataAccess. types; using Oracle. dataAccess; using DBHelper. interface; namespace DBHelper {public class ORacleDBHelp: ITransDB {private static OracleConnection conn; private bool IsTran = false; private OracleConnection TranConn; private OracleCommand cmd; private OracleTransaction Transaction; # region transaction processing method // <summary> // enable global transaction processing /// </summary> public void BeginTransaction () {TranConn = new OracleConnection (ConfigurationManager. connectionStrings ["OracleConnection"]. connectionString); if (TranConn. state = ConnectionState. closed) {TranConn. open ();} Transaction = TranConn. beginTransaction (); cmd = new OracleCommand ("", TranConn); cmd. commandTimeout = 600; IsTran = true ;}/// <summary> /// submit global transaction processing /// </summary> public void Commit () {cmd. transaction. commit (); // event Submit Transaction. dispose (); cmd. dispose (); TranConn. close (); TranConn. dispose () ;}//< summary> /// roll back global transaction processing /// </summary> public void Rollback () {if (IsTran) {cmd. transaction. rollback (); // Transaction Rollback Transaction. dispose (); cmd. dispose (); TranConn. close (); TranConn. dispose () ;}/// <summary> // execute an SQL statement without parameters, returns the number of affected rows /// </summary> /// <param name = "shortstr"> add, delete, modify the SQL statement </param> /// <returns> returns the number of affected rows. </re Turns> public int TranExecuteNonQuery (string plain text) {int count; try {cmd. commandText = plain text; cmd. commandTimeout = 600; if (cmd. connection. state = ConnectionState. closed) {cmd. connection. open ();} count = cmd. executeNonQuery ();} catch (Exception ex) {throw new Exception (ex. message. toString (). trim () ;}return count ;}# endregion # region creates a database connection object /// <summary> /// create a database connection /// </summary> /// <Returns> returns a database connection OracleConnection object </returns> public static OracleConnection init () {try {if (conn = null) {conn = new OracleConnection (ConfigurationManager. connectionStrings ["OracleConnection"]. toString (). trim ();} if (conn. state! = ConnectionState. open) {conn. open () ;}} catch (Exception e) {try {Thread. sleep (5000); conn = new OracleConnection (ConfigurationManager. connectionStrings ["OracleConnection"]. toString (). trim (); if (conn. state! = ConnectionState. open) {conn. open () ;}} catch (Exception) {throw new Exception (e. message. toString (). trim () ;}} return conn ;}# endregion /// <summary> /// execute an SQL statement without parameters, returns the number of affected rows /// </summary> /// <param name = "shortstr"> add, delete, modify the SQL statement </param> /// <returns> return the affected number of rows </returns> public static int ExecuteNonQuery (string plain text) {int count; OracleCommand ocmd = null; try {init (); ocmd = new OracleCommand ("", conn); ocmd. commandTimeout = 600; ocmd. commandText = plain text; count = ocmd. executeNonQuery ();} catch (Exception ex) {throw new Exception (ex. message. toString (). trim ();} return count;} // <summary> // execute an SQL statement without parameters, returns an OracleDataReader object that reads data from the data source /// </summary> /// <param name = "shortstr"> corresponding SQL statement </param> /// <returns> return an OracleDataReader object for reading data from the data source </returns> public static DataTable GetDataTable1 (string plain text) {OracleDataReader reader; OracleCommand ocmd = null; DataTable dt = new DataTable (); try {init (); ocmd = new OracleCommand ("", conn); ocmd. commandTimeout = 600; ocmd. commandText = plain text; ocmd. commandTimeout = 600; reader = ocmd. executeReader (); dt. load (reader); reader. dispose ();} catch (Exception ex) {throw new Exception (ex. message. toString (). trim () ;}return dt ;}}}View Code

ITransDB Interface

/* ===================================================== ========================================================== * Function description: ITransDB * Creator: Zouqj * creation date: 14:31:32 ===================================================== ========================================================== */using System; using System. collections. generic; using System. linq; using System. text; namespace DBHelper. interface {public interface ITransDB {void BeginTransaction (); int TranExecuteNonQuery (string SQL); void Commit (); void Rollback ();}}

Now you can use C # To directly call oracle operations.

Next, connect to the Oracle server through the Oracle client

After Navicat For Oracle is installed, oracle is not installed on our computer. Copy the downloaded dll to the installation directory of Navicat For Oracle. on my computer, D: \ Program Files (x86) \ PremiumSoft \ Navicat for Oracle \ instantclient_10_2 \

Then configure Navicat For Oracle, "tool" -- "option"

At this time, Navicat For Oracle can connect to the Oracle database on the server.

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.