c#远程连接oracle(本地无oracle客户端)

我的操作系统是win10 x64 ,服务器oracle版本是g11

服务器上oracle做一些配置

然后更改tnsnames.ora

添加你的配置

我的例如:

ddd=zkrOracle108 =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.51.108)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SID = DB11G)

    )
  ) 

配置环境变量

 NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK 

采用OracleClient进行连接数据库

在oracle官网上下载Instant Client 程序包

地址:http://www.oracle.com/technetwork/cn/topics/winx64soft-101515-zhs.html

然后放到最后生成的dll文件夹中,例如debug

然后程序代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;
using System.Configuration;
using System.Data;
namespace ConsoleDBTool
{
    class Program
    {
        static void Main(string[] args)
        {
            string connstr = ConfigurationManager.ConnectionStrings["ORACLEConnectionString"].ConnectionString;
            //todo:SERVER写成SERVER_NAME会报出 ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务
            //password错误会提示:ORA-01017: 用户名/口令无效; 登录被拒绝 
            //没有写(SID = DB11G)会提示:ORA-12504: TNS: 监听程序在 CONNECT_DATA 中未获得 SERVICE_NAME
            //没写(SERVER=DEDICATED)会提示:妈的!可以成功返回
            //没写DESCRIPTION =(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.51.108)(PORT = 1521)))  ORA-12533: TNS: 非法的 ADDRESS 参数
            //HOST地址配错一般会提示连接超时
            //从网上复制的这种 string sqlcon = @"Data Source=TestDB;Persist Security Info=True;User ID=tanyixiu;Password=pwd;Unicode=True";,他妈的绝对是不行的!
            //只要提示是DRA开头,说明你已经和oracle发生“握手”了,如果出现System.Data.OracleClient 需要 Oracle 客户端软件 version 8.1.7 或更高版本说明没有完成Instant Client的安装或环境变量的配置
            //从昨天下午开始搞这个,到16:00才连上oracle,我觉得是上帝玩够了,不想和你开玩笑了,所以你就成功了。
            //var connectionString = @"Data Source=(DESCRIPTION =(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.51.108)(PORT = 1521)))(CONNECT_DATA =(SERVER = DEDICATED)(SID = DB11G)));User Id=123;Password=123;Persist Security Info=True;";//

            OracleConnection oc = new OracleConnection(connstr);
            oc.Open();
            OracleDataAdapter od = new OracleDataAdapter("select * from dir_test", connstr);
            DataSet ds = new DataSet();
            od.Fill(ds);
            Console.WriteLine("数据库连接成功");
        }
    }
}

config:

<?xml version="1.0"?>
<configuration>
  <startup>
    <supportedRuntime version="v2.0.50727"/>
  </startup>
  <connectionStrings>
    <add name="ORACLEConnectionString" connectionString="Data Source=(DESCRIPTION =(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.51.108)(PORT = 1521)))(CONNECT_DATA =(SERVER = DEDICATED)(SID = DB11G)));User Id=123;Password=123;Persist Security Info=True;"/>
  </connectionStrings>
</configuration>

 

posted @ 2016-07-28 17:09  IT火  阅读(1671)  评论(0编辑  收藏  举报