0

我的盒子上安装了 32 位驱动程序(它们是由一些 DBA 安装和配置的)

我写了一个简单的脚本来测试驱动程序,大致如下

using (DataTable table = new DataTable())
{
    using (OracleConnection connection = new OracleConnection())
    {
        connection.ConnectionString = "Data Source=alias;User id=user;Password=password";
        connection.Open();
        using (OracleCommand command = connection.CreateCommand())
        {
            command.CommandType = CommandType.Text;
            command.CommandText = "SELECT * FROM DUAL";
            table.Load(command.ExecuteReader());
        }
    }
}

当我使用 32 位 Oracle.DataAccess.dll 将此应用程序编译为 32 位时,它可以顺利执行。

但是,如果我使用 Oracle.ManagedDataAccess.dll 将应用程序编译为 AnyCPU,我会收到 ORA-12154(无法解析指定的连接标识符)错误。

如果我 tnsping 别名,它可以正常工作并告诉我连接标识符和真实数据库名称。

如果我随后更改连接字符串以使用真实的数据库名称而不是别名,然后再次尝试使用托管库,它将顺利执行。

我一直在阅读并发现这个答案说托管驱动程序依赖于 tnsnames.ora 文件来解析别名,但是我依赖于 sqlnet.ora 和 ldap.ora 中定义的 LDAP 服务器。

当我 tnsping 时,它说它使用 sqlnet.ora 来解析名称。

那么托管驱动程序为什么不起作用呢?

4

2 回答 2

1

愿此解决方法适合您。您可以自己查询 LDAP 服务器并将完整的连接字符串放入您的代码中。

您可以使用以下代码从 LDAP 解析连接字符串:

using (OracleConnection connection = new OracleConnection())
{
    connection.ConnectionString = "Data Source=" + ResolveServiceNameLdap("alias") + ";User id=user;Password=password";
    connection.Open();
}

...

  private static string ResolveServiceNameLdap(string serviceName)
  {
     string tnsAdminPath = Path.Combine(@"C:\oracle\network", "ldap.ora");
     // or something more advanced...

     // ldap.ora can contain many LDAP servers
     IEnumerable<string> directoryServers = null;

     if ( File.Exists(tnsAdminPath) ) {
        string defaultAdminContext = string.Empty;

        using ( var sr = File.OpenText(tnsAdminPath) ) {
           string line;

           while ( ( line = sr.ReadLine() ) != null ) {
              // Ignore comments or empty lines
              if ( line.StartsWith("#") || line == string.Empty )
                 continue;

              // If line starts with DEFAULT_ADMIN_CONTEXT then get its value
              if ( line.StartsWith("DEFAULT_ADMIN_CONTEXT") )
                 defaultAdminContext = line.Substring(line.IndexOf('=') + 1).Trim(new[] { '\"', ' ' });

              // If line starts with DIRECTORY_SERVERS then get its value
              if ( line.StartsWith("DIRECTORY_SERVERS") ) {
                 string[] serversPorts = line.Substring(line.IndexOf('=') + 1).Trim(new[] { '(', ')', ' ' }).Split(',');
                 directoryServers = serversPorts.SelectMany(x => {
                    // If the server includes multiple port numbers, this needs to be handled
                    string[] serverPorts = x.Split(':');
                    if ( serverPorts.Count() > 1 )
                       return serverPorts.Skip(1).Select(y => string.Format("{0}:{1}", serverPorts.First(), y));

                    return new[] { x };
                 });
              }
           }
        }

        // Iterate through each LDAP server, and try to connect
        foreach ( string directoryServer in directoryServers ) {
           // Try to connect to LDAP server with using default admin contact
           try {
              var directoryEntry = new DirectoryEntry("LDAP://" + directoryServer + "/" + defaultAdminContext, null, null, AuthenticationTypes.Anonymous);
              var directorySearcher = new DirectorySearcher(directoryEntry, "(&(objectclass=orclNetService)(cn=" + serviceName + "))", new[] { "orclnetdescstring" }, SearchScope.Subtree);
              SearchResult searchResult = directorySearcher.FindOne();
              var value = searchResult.Properties["orclnetdescstring"][0] as byte[];
              if ( value != null )
                 connectionString = Encoding.Default.GetString(value);

              // If the connection was successful, then not necessary to try other LDAP servers
              break;
           } catch {
              // If the connection to LDAP server not successful, try to connect to the next LDAP server
              continue;
           }
        }

        // If casting was not successful, or not found any TNS value, then result is an error 
        if ( string.IsNullOrEmpty(connectionString) ) 
           throw new Exception("TNS value not found in LDAP");

     } else {
        // If ldap.ora doesn't exist, then throw error 
        throw new FileNotFoundException("ldap.ora not found");
     }

     return connectionString;
  }
于 2015-06-18T16:25:31.727 回答
0

ODP.NET 托管驱动程序依赖于在配置文件中设置的 TNS_ADMIN 值来查找 TNSNAMES.ORA 文件。如果您不想设置该值,可以将 TNSNAMES.ORA 文件包含在可执行文件的工作目录中,或者直接在配置文件中创建 TNS 别名。

编辑:如果您依赖 SQLNET.ORA 和 LDAP.ORA,您也可以将它们复制到工作目录中,或者在配置文件中设置 LDAP 参数。有关 LDAP 可用的配置文件参数,请参阅 ODP.NET 文档。

于 2015-06-18T05:40:51.537 回答