oracle 如何在 C# 应用程序中设置 TNSNAMES 文件的正确路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10618512/
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 00:49:36  来源:igfitidea点击:

How to set proper path to TNSNAMES file in C# application?

c#oracletnsnames

提问by Marshall

I have a program in C# that use ODP.NET dlls:

我有一个使用 ODP.NET dll 的 C# 程序:

oci.dll, ociw32.dll, Oracle.DataAccess.dll,
orannzsbb11.dll, oraocci11.dll, oraociicus11.dll,
OraOps11w.dll. 

I've got 2 computers. First with whole ODAC package installed, and second without that package. But I have all required dlls in my exe directory, so ODAC is not a problem I think.

我有2台电脑。第一个安装了整个 ODAC 包,第二个没有那个包。但是我的 exe 目录中有所有必需的 dll,所以我认为 ODAC 不是问题。

The difference between these computers is the path to the TNSNAMESfile.

这些计算机之间的区别在于TNSNAMES文件的路径。

First: C:\app\OraHome_1\Network\admin\
Second: C:\Oracle\product.2.0\client_1\network\admin

On the first computer, the program works fine. But on the second one with the same connection string, I get the error:

在第一台计算机上,该程序运行良好。但是在具有相同连接字符串的第二个上,我收到错误消息:

cannot open connection (ORA-12154)

Using SQL Plus I can connect on both computers. How can I show my program the proper path to the tnsnames.orafile?

使用 SQL Plus 我可以在两台计算机上连接。如何向我的程序显示tnsnames.ora文件的正确路径?

回答by SQLMason

You can set the TNS_ADMINenvironment variable programmatically. See this page for a step by step.That is if you wanted to change to a specific TNS_NAMES.ORAfile. The Oracle Client must still be installed on the client machine.

您可以通过TNS_ADMIN编程方式设置环境变量。请参阅此页面以逐步了解。也就是说,如果您想更改为特定TNS_NAMES.ORA文件。Oracle 客户端仍必须安装在客户端计算机上。

From ConnectionStrings- without using TNS:

来自ConnectionStrings- 不使用 TNS:

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



EDIT:编辑:添加了第三个选项

Please see this questionwhich could aid you in finding the current location of the client's TNS_NAMES.ORAfile - which you could open and modify if you wish (add your own connection if it doesn't exist)

参阅此问题,它可以帮助您找到客户端TNS_NAMES.ORA文件的当前位置- 您可以根据需要打开和修改该文件(如果它不存在,请添加您自己的连接)

回答by themarcuz

You don't need to care about the path of your TNSNames file: it'll be automatically discovered by the library itself... once you have it installed. That's the key point: distributing the dll within your project is not enough. You need to install ODP.Net on the machine that need to use it: actually the installation simply create a few registry entry, and one of them point to the right oracle dir (in which the library can find out the tnsnames when needed).

你不需要关心你的 TNSNames 文件的路径:它会被库本身自动发现......一旦你安装了它。这是关键点:在您的项目中分发 dll 是不够的。您需要在需要使用它的机器上安装 ODP.Net:实际上安装只是创建几个注册表项,其中之一指向正确的 oracle 目录(库可以在需要时在其中找到 tnsnames)。

Morover, as someone pointed out, you don't need a tnsnams file at all. You could write everything needed inside the connection string. Here's one I use in my environment:

Morover,正如有人指出的那样,您根本不需要 tnsnams 文件。您可以在连接字符串中写入所需的所有内容。这是我在我的环境中使用的一个:

Data Source= (DESCRIPTION =
      (ENABLE = BROKEN)
      (ADDRESS_LIST =
      (LOAD_BALANCE = ON)
      (FAILOVER = ON)
      (ADDRESS = (PROTOCOL = TCP)(Host =por10srv-a)(Port = 1521))
      )
      (CONNECT_DATA =
      (SERVICE_NAME = por10.gruppo.autostrade.it)
      (FAILOVER_MODE =
      (TYPE = SELECT)
      (METHOD = BASIC)
      (RETRIES = 10)
      (DELAY = 3)
      )
      )
      );User ID=npa_collaudo;Password=npa_collaudo;

回答by FidoFuz

You don't need to install ODP.NET (or for that matter the Oracle Client) as you seem to have the required DLLs for a local distributable inline oracle client. In your case it's possible to have the TNSNAMES.ORA file located in the same folder as your executable and your specialised "inline oracle client" will pick it up from there. Otherwise the oracle client local to your application will try to pick it up from any client installed on the machine.

您不需要安装 ODP.NET(或就此而言是 Oracle 客户端),因为您似乎拥有本地可分发内联 Oracle 客户端所需的 DLL。在您的情况下,可以将 TNSNAMES.ORA 文件与您的可执行文件位于同一文件夹中,并且您的专用“内联 oracle 客户端”将从那里获取它。否则,您的应用程序本地的 oracle 客户端将尝试从安装在机器上的任何客户端获取它。