利用instantclient_12_1免安装oracle客户端链接oracle

微笑此方法配置,无需重起服务器,非常方便 微笑


1、下载instantclient_12_1包,网上到处有,请自行搜索,下载后解压到自己喜欢的目录




2、本人写了一个环境变量自动增加的配置脚本(绝对原创!!!!!),自动添加oracle有关环境变量,请将脚本文本拷贝下来,保存为批处理文件,保存到上一步解压的目录下,



@echo.***************************************************
@echo.
@echo.    windows 2008 下设置oracle相关环境变量,
@echo.    请用管理员权限运行
@echo.
@echo.
@echo.***************************************************


@echo off


set regPath="HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment"


::echo %regpath%
::pause


::增加当前路径到path中
set key=Path
::判断是否存在该路径
reg query %regPath% /v %key% 1>nul 2>nul
if %ERRORLEVEL%==0 (
::取值
For /f "tokens=3,4 delims= " %%v in ('Reg Query %regPath% /v %key% ') do Set oldValue=%%v
) else Set oldValue=""
@echo %key%的原值为:%oldValue%


::添加新path值
set newValue="%oldValue%;"%~sdp0"
@echo %key%的新值为%newValue%
setx %key% /M %newValue%


::设置oracle的其他环境变量
setx ORACLE_HOME /M "%~sdp0"
setx TNS_ADMIN /M "%~sdp0network\ADMIN"
setx NLS_LANG /M "SIMPLIFIED CHINESE_CHINA.ZHS16GBK"
@echo.


::显示修改后的环境变量


set key=Path
reg query %regPath% /v %key% 1>nul 2>nul
if %ERRORLEVEL%==0 (
For /f "tokens=3,4 delims= " %%v in ('Reg Query %regPath% /v %key% ') do Set oldValue=%%v
) else Set oldValue=""
@echo 修改后%key%的值为:%oldValue%
@echo.


set key=ORACLE_HOME
reg query %regPath% /v %key% 1>nul 2>nul
if %ERRORLEVEL%==0 (
For /f "tokens=3,4 delims= " %%v in ('Reg Query %regPath% /v %key% ') do Set oldValue=%%v
) else Set oldValue=""
@echo 修改后%key%的值为:%oldValue%
@echo.


set key=TNS_ADMIN
reg query %regPath% /v %key% 1>nul 2>nul
if %ERRORLEVEL%==0 (
For /f "tokens=3,4 delims= " %%v in ('Reg Query %regPath% /v %key% ') do Set oldValue=%%v
) else Set oldValue=""
@echo 修改后%key%的值为:%oldValue%
@echo.




set key=NLS_LANG
reg query %regPath% /v %key% 1>nul 2>nul
if %ERRORLEVEL%==0 (
For /f "tokens=3,4 delims= " %%v in ('Reg Query %regPath% /v %key% ') do Set oldValue=%%v
) else Set oldValue=""
@echo 修改后%key%的值为:%oldValue%
@echo.






@echo.***************************************************
@echo 处理结束,请按任意键退出
@pause >nul


3、用管理员权限运行该脚本,会自动加上oracle有关的变量。脚本在windows2008下测试通过,其他系统未测试,如下图所示,在系统属性里可以看到加好的环境变量





4、plsql的配置,请参照下图操作





5、Navicat的配置,参照下图



6、c#访问oracle,请参照源代码如下


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data.OleDb;
using System.Data;

namespace ConsoleApplication10
{
    class Program
    {
        public static OleDbConnection conn = null;  
        static void Main(string[] args)
        {
            OraTest01();
        }
       
        static void OraTest01()
        {
            oledbLink225();  //打开数据库连接
            DataSet da = ReturnDataSet("select * from aaaa");
            if ( (da.Tables.Count >= 1 ) || (da == null) )
            {
                Console.WriteLine(da.Tables[0].Rows.Count.ToString());
            }
            Close();
            Console.ReadKey();
        }
    
        public static void oledbLink225()
        {
            string connString = @"Provider=MSDAORA.1;
User ID=XXXX;Password=XXXX;
Data Source=(
DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = XXX.XXX.XXX.XXX)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = ORCL)
    )
)";

            conn = new OleDbConnection(connString);
            try
            {
                conn.Open();
                Console.WriteLine(  "ora is " + conn.State.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }

        public static void Close()
        {
            if (conn != null)
            {
                conn.Close();
                conn.Dispose();
                conn = null;
                Console.WriteLine("数据库关闭成功");
            }
        }

        public static DataSet ReturnDataSet(string sql)
        {
            DataSet da = new DataSet();
            try
            {
                OleDbCommand cmd = conn.CreateCommand();
                if (conn.State == ConnectionState.Closed || conn.State == ConnectionState.Broken)
                {
                    conn.Open();
                }
                cmd.CommandText = sql;      //SQL语句 

                OleDbDataAdapter oda = new OleDbDataAdapter(sql, conn);
                oda.Fill(da);
            }
            catch
            {
                Console.WriteLine("数据读取数据失败");
            }

            return da;
        }
      
    }
}


注意:一定要将项目-属性-生成-目标平台 改为x86  (32位程序)





如果没有改,是any cpu或x64,则无法调用msdoara.1.1和数据库建立链接



已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页