電腦上不安裝Oracle時,C# 調用oracle數據庫,Oracle客戶工具


Oracle的安裝包通常都比較大,安裝又比較費時,而且如果安裝過程中不幸出錯,各種蛋疼,即便是安裝過N遍的老手,有時候安裝起來也覺得挺煩。而工作中,通常服務器上面安裝oracle就可以了,我們本地電腦只需要安裝一個oracle客戶端,然后連接到服務器就可以了。

Oracle 輕量級客戶端我推薦使用Navicat For Oracle,只有17M。

1、下載以下幾個dll,我這里使用的是oracle11g,這幾個DLL的下載地址:http://pan.baidu.com/s/1kU1JafX

2、將其拷貝到項目中,設置dll屬性為“復制到輸出目錄”為“始終復制”

3、項目中添加Oracle.DataAccess.dll的引用

4、創建oracle操作類ORacleDBHelp

/* ==============================================================================
   * 功能描述:ORacleDBHelp  
   * 創 建 者:Zouqj
   * 創建日期:2015/9/9 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.DataAccess.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 事務處理方法
        /// <summary>
        /// 開啟全局事務處理
        /// </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>
        /// 提交全局事務處理
        /// </summary>
        public void Commit()
        {
            cmd.Transaction.Commit();//事務提交
            Transaction.Dispose();
            cmd.Dispose();
            TranConn.Close();
            TranConn.Dispose();
        }

        /// <summary>
        /// 回滾全局事務處理
        /// </summary>
        public void Rollback()
        {
            if (IsTran)
            {
                cmd.Transaction.Rollback();//事務回滾
                Transaction.Dispose();
                cmd.Dispose();
                TranConn.Close();
                TranConn.Dispose();
            }
        }
        /// <summary>
        /// 執行不帶參數sql語句,返回所影響的行數
        /// </summary>
        /// <param name="cmdstr">增,刪,改sql語句</param>
        /// <returns>返回所影響的行數</returns>
        public int TranExecuteNonQuery(string cmdText)
        {
            int count;
            try
            {
                cmd.CommandText = cmdText;
                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 建立數據庫連接對象
        /// <summary>
        /// 建立數據庫連接
        /// </summary>
        /// <returns>返回一個數據庫的連接OracleConnection對象</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>
        /// 執行不帶參數sql語句,返回所影響的行數
        /// </summary>
        /// <param name="cmdstr">增,刪,改sql語句</param>
        /// <returns>返回所影響的行數</returns>
        public static int ExecuteNonQuery(string cmdText)
        {
            int count;
            OracleCommand ocmd = null;
            try
            {
                init();
                ocmd = new OracleCommand("", conn);
                ocmd.CommandTimeout = 600;
                ocmd.CommandText = cmdText;
                count = ocmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString().Trim());
            }
            return count;
        }
        /// <summary>
        /// 執行不帶參數sql語句,返回一個從數據源讀取數據的OracleDataReader對象
        /// </summary>
        /// <param name="cmdstr">相應的sql語句</param>
        /// <returns>返回一個從數據源讀取數據的OracleDataReader對象</returns>
        public static DataTable GetDataTable1(string cmdText)
        {
            OracleDataReader reader;
            OracleCommand ocmd = null;
            DataTable dt = new DataTable();
            try
            {
                init();
                ocmd = new OracleCommand("", conn);
                ocmd.CommandTimeout = 600;
                ocmd.CommandText = cmdText;
                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接口

/* ==============================================================================
   * 功能描述:ITransDB  
   * 創 建 者:Zouqj
   * 創建日期:2015/9/9 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();
    }
}

現在可以通過C#來直接調用oracle操作了。

接下來,通過Oracle客戶端連接到Oracle服務器

安裝完Navicat For Oracle后,我們電腦上面沒有安裝oracle,將下載的dll拷貝到Navicat For Oracle的安裝目錄,我電腦上面是:D:\Program Files (x86)\PremiumSoft\Navicat for Oracle\instantclient_10_2\

然后配置Navicat For Oracle,“工具”——“選項”

這個時候,Navicat For Oracle就可以連接服務器上面的Oracle數據庫了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM