gpt4 book ai didi

c# - 使用 Oracle.ManagedDataAccess 连接到 Oracle

转载 作者:可可西里 更新时间:2023-11-01 08:06:08 24 4
gpt4 key购买 nike

我在我的 C# (.NET 4.0) 项目中使用 Oracle.ManagedDataAccess Nuget 包版本 12.1.022。该包会自动在 app.config 文件中创建条目。我如何从该文件中读取数据源字符串以便能够连接到数据库?

System.Configuration.ConfigurationManager.AppSettings.Get("dataSource");

不适合我

<?xml version="1.0" encoding="utf-8"?>

<configuration>
<configSections>
<section name="oracle.manageddataaccess.client"
type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.121.2.0, 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.121.2.0, 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.121.0.0 - 4.65535.65535.65535" newVersion="4.121.2.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>

<oracle.manageddataaccess.client>
<version number="*">
<dataSources>
<dataSource alias="MyDataSource"
descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=100.100.100.100)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=myservice.com)))" />
</dataSources>
</version>
</oracle.manageddataaccess.client>
</configuration>

最佳答案

通常,您会在标准连接字符串中引用别名:

  <connectionStrings>
<add name="MyConnection" connectionString="Data Source=MyDataSource;User Id=scott;Password=tiger;"/>
</connectionStrings>

然后您将使用标准方法来检索字符串:

ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;

另请记住,数据源部分中的别名是可选的。您可以将描述符直接嵌入到连接字符串中:

  <connectionStrings>
<add name="MyConnection" connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=100.100.100.100)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=myservice.com)));User Id=scott;Password=tiger;"/>
</connectionStrings>

您还可以在 tnsnames.ora 文件中引用别名。默认情况下,驱动程序在 exe 文件夹、TNS_ADMIN 环境变量或 TNS_ADMIN 配置变量中指定的目录中查找 tnsnames.ora:

http://docs.oracle.com/cd/E48297_01/doc/win.121/e41125/featConfig.htm#autoId6 http://docs.oracle.com/cd/E48297_01/doc/win.121/e41125/featConfig.htm#autoId7

关于c# - 使用 Oracle.ManagedDataAccess 连接到 Oracle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31564288/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com