扩大
缩小

电脑上不安装Oracle时,C# 调用oracle数据库,Oracle客户工具 【转载】

http://www.cnblogs.com/jiekzou/p/5047850.html

 

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;
        }
    }
}
复制代码

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数据库了。

posted on 2018-04-16 17:40  禅道  阅读(326)  评论(0编辑  收藏  举报

导航