zoukankan      html  css  js  c++  java
  • ADO.Net连接Oracle

    1、添加 Oracle.ManagedDataAccess.dll

    2、连接Oracle的实例得添加到Oracle的监听器中,不然会报“ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务”的错,添加如下,本例使用的是Oracle10g,找到C:oracleproduct10.2.0db_1NETWORKADMIN下的lisener.org,打开后修改添加标红的部分

    # listener.ora Network Configuration File: C:oracleproduct10.2.0db_1
    etworkadminlistener.ora
    # Generated by Oracle configuration tools.
    
    SID_LIST_LISTENER =
      (SID_LIST =
        (SID_DESC =
          (SID_NAME = PLSExtProc)
          (ORACLE_HOME = C:oracleproduct10.2.0db_1)
          (PROGRAM = extproc)
        )
        (SID_DESC =
          (GLOBAL_DBNAME = ORCL)
          (ORACLE_HOME = C:oracleproduct10.2.0db_1)
          (SID_NAME = ORCL)
        )
      )
    
    LISTENER =
      (DESCRIPTION_LIST =
        (DESCRIPTION =
          (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
          (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.121.130)(PORT = 1521))
        )
      )
    

      3、连接字符串:

    string connStr = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.121.130)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL)));Persist Security Info=True;User ID=scott;Password=tiger;";

    其中Oracle数据库服务器IP:192.168.121.130

    ServiceName:ORCL

    用户名:scott

    密码:tiger

    4、查看Oracle的实例名,用sys作为dba登录,执行语句 select name from v$database;

    5,C#代码

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using Oracle.ManagedDataAccess.Client;
    using System.Linq;
    using System.Text;
    
    namespace ADOSample
    {
        class Program
        {
            static void Main(string[] args)
            {          
                string connStr = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.121.130)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL)));Persist Security Info=True;User ID=scott;Password=tiger;";
                using (OracleConnection conn = new OracleConnection(connStr))
                {
                    string sql = "select * from dept ";
                    conn.Open();
                    using (OracleCommand cmd = new OracleCommand(sql, conn))
                    {
                        using (OracleDataReader dataReader = cmd.ExecuteReader())
                        {
                            while (dataReader.Read())
                            {
                                string obj = (string)dataReader[1];
                                Console.WriteLine(obj);
                            }
                        }                    
    
                    }
                }
                Console.ReadKey();
            }
        }
    }
    

      

  • 相关阅读:
    sudo 之后 unable to resolve host的问题解决办法
    Linux 查找具体的文件名称
    linux 访问远程务器代码
    spark 安装配置
    R基本介绍
    BIEE多层表头报表的制作方法
    支付宝新漏洞引发恐慌,那如何关闭小额免密支付呢
    大家注意了,支付宝被曝重大安全漏洞,回应称正在跟进排查
    2017年5个不应该被忽视的机器学习项目
    婚前最后一次加班
  • 原文地址:https://www.cnblogs.com/javabg/p/7231121.html
Copyright © 2011-2022 走看看