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

情到浓时终转凉″ 提交于 2019-11-30 21:04:00
SQLMason

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

From ConnectionStrings - without using TNS:

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


EDIT: Added 3rd option

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

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).

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:

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;

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!