繁体   English   中英

如何使用 LDAP/OID 连接字符串通过 C# 连接到 Oracle

[英]How to connect to Oracle with C# using a LDAP/OID connection string

关于使用 C#.NET 连接到 Oracle 数据库,我有两个问题。

我的程序在我的 Oracle 19c 客户端 64 位文件夹中引用了 Oracle.DataAccess.dll(版本 2.122.19.1)。

我有一个使用语句:

使用 Oracle.DataAccess.Client;

和此代码连接:

string connectionString = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=myserver.sys.mycompany.com)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=mydb.mycompany.com)));User Id=MyLanId;Password=MyPasswors;Validate Connection=True;"

using (OracleConnection connection = new OracleConnection(connectionString))
{
    connection.Open();
}

问题 #1:

如何创建一个 OID 连接字符串,它不指定数据库名称,而是查询 LDAP 以获取在上面的无 TNS 连接字符串中硬编码的详细服务器/端口信息?

比如在Java程序中,我们是这样连接的。

base.oracle.db.url=jdbc:oracle:thin:@ldap://oid.gtm.internal.mycompany.com:3060/dbname,cn=OracleContext,dc=mycompany,dc=com

.NET 的等价物是什么?

问题 #2:

当我编译我的 C# 程序时,我选择了“Any CPU”,但我的程序仅在我引用 64 位 Oracle.DataAccess.dll 时才有效。 这是因为我的 PATh 变量在我的 32 位文件夹之前提到了我的 64 位 oracle 客户端文件夹(我都有)? 现在,我的 PATH 变量超过了 2048 的最大长度,如果不缩短它我就无法更改它,而且我不确定要删除什么。

我发现了这个 SO 帖子,

如何在使用托管 ODP.NET 时从 C# 查询 LDAP 以解析 Oracle TNS 主机名?

除了传递指向 LDAP 服务器的连接字符串外,我还可以执行 LDAP 查询来获取数据库描述符以构建连接字符串。

这是一个两步过程,所以我可能想在构建连接字符串后缓存它。 当我完成它们时,我通常指望连接池和“关闭”连接,但是这两个步骤的过程似乎我必须手动添加一些连接字符串的缓存以避免多次点击 LDAP 的开销。

有没有更好的办法?

    string directoryServer = "oid.gtm.internal.mycompany.com:3060";
    string defaultAdminContext = "cn=OracleContext,dc=mycompany,dc=com";
    string serviceName = "mydb";
    string userId = "mylanid";
    string password = "mypwd";

    using (IDbConnection connection = GetConnection(directoryServer, defaultAdminContext, serviceName, userId, password))
    {
        connection.Open();

        connection.Close();
    }
    
private static IDbConnection GetConnection(string directoryServer, string defaultAdminContext,
                                           string serviceName, string userId, string password)
{
    string descriptor = ConnectionDescriptor(directoryServer, defaultAdminContext, serviceName);
    // Connect to Oracle
    string connectionString = $"user id={userId};password={password};data source={descriptor}";
    OracleConnection con = new OracleConnection(connectionString);
    return con;
}

private static string ConnectionDescriptor(string directoryServer, string defaultAdminContext,
    string serviceName)
{
    string ldapAdress = $"LDAP://{directoryServer}/{defaultAdminContext}";
    string query = $"(&(objectclass=orclNetService)(cn={serviceName}))";
    string orclnetdescstring = "orclnetdescstring";

    DirectoryEntry directoryEntry = new DirectoryEntry(ldapAdress, null, null, AuthenticationTypes.Anonymous);
    DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry, query, new[] { orclnetdescstring },
        SearchScope.Subtree);

    SearchResult searchResult = directorySearcher.FindOne();
    byte[] value = searchResult.Properties[orclnetdescstring][0] as byte[];

    if (value != null)
    {
        string descriptor = Encoding.Default.GetString(value);
        return descriptor;
    }

    throw new Exception("Error querying LDAP");
}

成功创建 oracle 连接后

OracleConnection con = new OracleConnection(connectionString);

如何使用sql从中获取数据:

string strSQL="select * from users";

我使用以下代码没有数据返回

  con.Open();
  OleDbDataAdapter oDA = new OleDbDataAdapter(strSQL, con);
  DataSet ds = new DataSet();
  oDA.Fill(ds);
  con.Close();

请指教。

暂无
暂无

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

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