简体   繁体   English

如何使用 LDAP 为 ORACLE 设置 C# 连接字符串以指定数据库?

[英]How can I setup a C# connection string for ORACLE using LDAP to specify the database?

I have a connection string to a Oracle database that looks like this:我有一个到 Oracle 数据库的连接字符串,如下所示:

Data Source=myserver:1521/myservice;User ID=MY_SCHEMA;Password=mypassword

I understand this is basically a TNS-less way of specifying the database we are connecting too.我知道这基本上是一种指定我们正在连接的数据库的无 TNS 方式。 It works, but normally we refer our databases as they are specified in LDAP.它有效,但通常我们引用我们的数据库,因为它们在 LDAP 中指定。 Especially useful if a database is moved to a new location, we only have to update the LDAP entry.如果将数据库移动到新位置特别有用,我们只需更新 LDAP 条目。 I've tried it like this:我试过这样:

Data Source=MYLDAPENTRY;User ID=MY_SCHEMA;Password=mypassword

However, when I try to connect this way I get an但是,当我尝试以这种方式连接时,我得到一个

ORA-12154: TNS:could not resolve the connect identifier specified

A TNSPING of MYLDAPENTRY gives me back一个 MYLDAPENTRY 的 TNSPING 让我回来

Used LDAP adapter to resolve the alias
Attempting to contact (DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=myserver)(PORT=1521)))
(CONNECT_DATA=(SERVICE_NAME=myservice)))
OK (140 msec)

Which matches the TNS-less connection I'm able to get working.这与我能够开始工作的无 TNS 连接相匹配。

Connecting with SQLPLUS on the same server/workstation works fine:在同一服务器/工作站上与 SQLPLUS 连接工作正常:

sqlplus MY_SCHEMA/mypassword@MYLDAPENTRY 

I would expect given the error, it's defaulting to searching TNSNames.ora instead of consulting LDAP.ora and querying the indicated LDAP server我希望考虑到错误,它默认搜索 TNSNames.ora 而不是咨询 LDAP.ora 并查询指定的 LDAP 服务器

Is there a different way to specify it when dealing with an LDAP server;在处理 LDAP 服务器时是否有不同的方式来指定它; or something I have to configure in OPT.NET?或者我必须在 OPT.NET 中配置什么?

This gave me a clue... https://stackoverflow.com/a/41571809/685这给了我一个线索...... https://stackoverflow.com/a/41571809/685

I am using the ODP.Net Managed Driver.我正在使用 ODP.Net 托管驱动程序。 It won't read the TNS_ADMIN environment variable nor the equivalent registry key.它不会读取 TNS_ADMIN 环境变量,也不会读取等效的注册表项。 So you have two options to resolve this:所以你有两个选择来解决这个问题:

[1] You can specify the TNS_ADMIN value in one of your .config files (e,g, app.config, web.config, and machine.config) [1] 您可以在 .config 文件之一(例如 app.config、web.config 和 machine.config)中指定 TNS_ADMIN 值

<oracle.manageddataaccess.client>
    <version number="*">
        <settings>
            <setting name="TNS_ADMIN" value="C:\oracle\work"/>
        </settings>
    </version>
</oracle.manageddataaccess.client>

Your mileage may vary depending on where the Oracle client is installed on your server or workstation (I know the path can vary depending on the version installed in my shop)您的里程可能会因 Oracle 客户端安装在您的服务器或工作站上的位置而异(我知道路径可能会因我商店中安装的版本而异)

Or或者

[2] You can specify the location of the LDAP server directly. [2] 您可以直接指定 LDAP 服务器的位置。

<oracle.manageddataaccess.client>
    <version number="*">
        <LDAPsettings>
            <LDAPsetting name="DIRECTORY_TYPE" value="AD" />
            <LDAPsetting name="DEFAULT_ADMIN_CONTEXT" value="DC=ad,DC=myDomain,DC=myTLD"/>
            <LDAPsetting name="DIRECTORY_SERVERS" value="(ad.myDomain.myTLD:PORTNUMBER)"/>
            <LDAPsetting name="DIRECTORY_SERVER_TYPE" value="AD"/>
        </LDAPsettings>
        <settings>
            <setting name="NAMES.DIRECTORY_PATH" value="(LDAP)"/>
            <setting name="NAMES.DEFAULT_DOMAIN" value="ad.myDomain.myTLD"/>
        </settings>
    </version>
</oracle.manageddataaccess.client>

In my case this is a LOT less likely to change between servers/workstations or over time as servers are replaced.在我的情况下,随着服务器的更换,服务器/工作站之间或随着时间的推移而发生变化的可能性很小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM