Oracle 连接数据库

使用的DLL:Oracle.ManagedDataAccess

Bug:OracleInternal.Common.ProviderConfig的类型初始值设定项引发异常

 

App.config的更改才是关键

 

关键应该是这个:

  oracle.manageddataaccess默认情况下只支持direct connection,
  如果要支持tns name,即ez connection,需要在app.config或者web.config里添加如下设置

 

 

安装后的App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="oracle.manageddataaccess.client"
      type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.18.3, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
  </configSections>
  <system.data>
    <DbProviderFactories>
      <remove invariant="Oracle.ManagedDataAccess.Client"/>
      <add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver"
        type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.122.18.3, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
    </DbProviderFactories>
  </system.data>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <publisherPolicy apply="no"/>
        <assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="89b483f429c47342" culture="neutral"/>
        <bindingRedirect oldVersion="4.122.0.0 - 4.65535.65535.65535" newVersion="4.122.18.3"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <oracle.manageddataaccess.client>
    <version number="*">
      <dataSources>
        <dataSource alias="SampleDataSource" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL))) "/>
      </dataSources>
    </version>
  </oracle.manageddataaccess.client>
</configuration>

 

更改后的App.config:

 

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.data>
    <DbProviderFactories>
      <remove invariant="Oracle.ManagedDataAccess.Client"/>
      <add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver"
        type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.122.18.3, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
    </DbProviderFactories>
  </system.data>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <publisherPolicy apply="no"/>
        <assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="89b483f429c47342" culture="neutral"/>
        <bindingRedirect oldVersion="4.122.0.0 - 4.65535.65535.65535" newVersion="4.122.18.3"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

 

 

去掉的节点 如下:

 

<configSections>
    <section name="oracle.manageddataaccess.client"
      type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.18.3, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
  </configSections>

 

  <oracle.manageddataaccess.client>
    <version number="*">
      <dataSources>
        <dataSource alias="SampleDataSource" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL))) "/>
      </dataSources>
    </version>
  </oracle.manageddataaccess.client> 

 

 

代码:

OracleHelper

 public class OracleHelper1
    {
        public static OracleCommand cmd = null;
        public static OracleConnection conn = null;
        public static string connstr = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.2.98)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=JJJT)));User Id=CMSP;Password=Oracle1";


        public static OracleConnection init()
        {
            try
            {
                conn = new OracleConnection(connstr);
                if (conn.State != ConnectionState.Open)
                {
                    conn.Open();
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message.ToString());
            }
            return conn;
        }

        public static DataSet GetDataSet(string sqlstr)
        {
            DataSet set = new DataSet();
            try
            {
                init();
                OracleDataAdapter adp = new OracleDataAdapter(sqlstr, conn);
                adp.Fill(set);
                conn.Close();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message.ToString());
            }
            return set;
        }

        public static int ExecuteNonQuery(string cmdText)
        {
            int count;
            try
            {
                init();
                cmd = new OracleCommand(cmdText, conn);
                count = cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            return count;
        }
    }
View Code

 

CS代码

DataTable dt1 = OracleHelper1.GetDataSet("select * from TB_PROJECTS").Tables[0];

 

转载于:https://www.cnblogs.com/guxingy/p/9620013.html

Toad for Oracle是一款常用的数据库管理工具,可以用于连接和管理Oracle数据库。要使用Toad for Oracle连接数据库,需要进行以下步骤: 1. 首先,确保你已经安装了Oracle数据库,并且知道数据库的连接信息,如主机名、端口号、数据库名称、用户名和密码。 2. 下载并安装Oracle Instant Client。你可以从Oracle官方网站的下载页面\[1\]上找到适合你操作系统的Instant Client版本,并按照官方的安装指南进行安装。 3. 配置环境变量。在安装完成后,需要将Oracle Instant Client的安装路径添加到系统的环境变量中。具体的配置方法可以参考相关的教程\[1\]。 4. 打开Toad for Oracle。在Toad for Oracle的连接窗口中,填写数据库的连接信息,包括主机名、端口号、数据库名称、用户名和密码。点击连接按钮,如果一切配置正确,你应该能够成功连接到Oracle数据库。 总结起来,要使用Toad for Oracle连接数据库,你需要下载并安装Oracle Instant Client,并配置好环境变量。然后,在Toad for Oracle中填写数据库的连接信息,即可连接到Oracle数据库。希望这些步骤对你有帮助! #### 引用[.reference_title] - *1* [Toad 连接 Oracle 数据库](https://blog.csdn.net/kmswilliam/article/details/115107328)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值