Oracle Managed ODP.NET 找不到 tnsnames.ora

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15275376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:29:55  来源:igfitidea点击:

Oracle Managed ODP.NET can't find tnsnames.ora

.netoracleodp.netmanaged

提问by SeanKilleen

My managed ODP.net webapp works locally but when deploying it to a server, it fails with the error:

我的托管 ODP.net web 应用程序在本地工作,但将其部署到服务器时,它失败并显示错误:

"TNS:listener does not currently know of service requested in connect descriptor"

“TNS:侦听器当前不知道连接描述符中请求的服务”

From looking around, it seems like this is because it can't get to the tnsnames.ora file.

环顾四周,似乎这是因为它无法访问tnsnames.ora文件。

I have tried the following with no success:

我尝试了以下方法但没有成功:

  • Placing a tnsnames.ora file (the same one that works locally) into an [oracle home][product]...\network\admin folder.
  • Setting a TNS_ADMIN setting in the Managed ODP's web.config section pointing to the environment variable.
  • Setting the TNS_ADMIN setting in the Managed ODP's web.config section pointing directly to the tnsnames.ora file.
  • 将 tnsnames.ora 文件(与本地工作相同的文件)放入 [oracle home][product]...\network\admin 文件夹。
  • 在指向环境变量的托管 ODP 的 web.config 部分中设置 TNS_ADMIN 设置。
  • 在 Managed ODP 的 web.config 部分中设置 TNS_ADMIN 设置,直接指向 tnsnames.ora 文件。

On the server, attempting to run tnsping yields error TNS-03502: Message 3502 not found; No message file for product=NETWORK, facility=TNS

在服务器上,尝试运行 tnsping 会产生错误 TNS-03502:未找到消息 3502;没有产品=NETWORK、设施=TNS 的消息文件

What am I missing?

我错过了什么?

回答by Branko Dimitrijevic

Try using a connection stringthat doesn't depend on tnsnames.ora, such as:

尝试使用不依赖于 tnsnames.ora的连接字符串,例如:

Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID)));User Id=myUsername;Password=myPassword;

回答by LcRok

Just adding the tns_admin path to web.config or app.config and point it to the folder where you have a tnsnames.ora file should work.

只需将 tns_admin 路径添加到 web.config 或 app.config 并将其指向您拥有 tnsnames.ora 文件的文件夹即可。

<oracle.manageddataaccess.client>
    <version number="*">
      <settings>
        <setting name="tns_admin" value="E:\oracle11\product.2.0\client_1\network\admin" />
      </settings>
    </version>
</oracle.manageddataaccess.client>

回答by Ian

An old post, but I've been looking for a similar solution.

一个旧帖子,但我一直在寻找类似的解决方案。

It occurs to me that as it looks like ODP.net doesn't allow for specifying a TNS file path, then if you are aware of the file path, just read the file programmatically and set the contents to the DataSource field of the ConnectionStringBuilder. Not ideal, but a reasonable workaround.

我突然想到 ODP.net 似乎不允许指定 TNS 文件路径,那么如果您知道文件路径,只需以编程方式读取文件并将内容设置为 ConnectionStringBuilder 的 DataSource 字段。不理想,但合理的解决方法。

回答by thom schumacher

I was after the same exact thing I ended up doing some regex on the TNSNAMES file. Once you've done the regex on the file you should be able to bring that into an object in Powershell or C#

我在做同样的事情,我最终在 TNSNAMES 文件上做了一些正则表达式。在文件上完成正则表达式后,您应该能够将其带入 Powershell 或 C# 中的对象中

param($tnsnamesPath = 'c:\tns\tnsnames.ora',$username = 'user',$password = 'gotmehere', $connectionName = 'mustard', $query = 'Select sysdate from dual')
$simplySQLPath = (Get-Module -ListAvailable simplySQL).ModuleBase
if($simplySQLPath -and (test-path $tnsnamesPath -PathType Leaf) -and (![string]::IsNullOrEmpty($node)))
{
    [System.Reflection.Assembly]::LoadFile("$simplySQLPath\DataReaderToPSObject.dll") | OUT-NULL
    Import-Module SimplySql -Force
    $parsedTN = (get-content $tnsnamesPath -raw)  -replace '(.*\=.*|\n.*\=)(.*|\n.*)\(DESCRIPTION*.\=' ,'Data Source = (DESCRIPTION ='  
    $splitTN = $parsedTN -split '(?=.*Data Source = \(DESCRIPTION \=)' 
    $tnsnames = $splitTN |?{$_ -like "*$connectionName*"}
    $connstring = "$tnsnames;User Id=$username;Password=$password"
    try
    {
        Open-OracleConnection -ConnectionString $connstring -ConnectionName $connectionName
        $result = Invoke-SqlQuery -ConnectionName $connectionName -Query "$SQLQuery"
        Close-SqlConnection -ConnectionName $connectionName
    }
    catch
    {
        $_.exception
    }

}
Else
{
    if(!(test-path $tnsnamesPath -PathType Leaf -ErrorAction Ignore))
    {
        Throw "Check TNSnamesPath:  $tnsNamesPath"
    }
    else
    {
        Throw "Exeception SIMPLYSQL not found in module Path $($env:PSModulePath)"
    }
}
$result

I've blogged about this code here: https://powershellposse.com/2018/03/13/tnsnames-file-parsing/

我在这里写了关于这段代码的博客:https: //powershellposse.com/2018/03/13/tnsnames-file-parsing/