C# ORA-12545:连接失败,因为目标主机或对象不存在 - System.Data.OracleClient 上没有错误

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

ORA-12545: Connect failed because target host or object does not exist - No error on System.Data.OracleClient

c#sqloracle

提问by Nick Williams

Can't believe I am having to ask this question, but I cannot for the life of me connect to an Oracle 11g database.

不敢相信我不得不问这个问题,但我一生都无法连接到 Oracle 11g 数据库。

The following works while using System.Data.OracleClient- but obviously this is depreciated. When using the exact same code but using Oracle.DataAccess.OracleClientI get the following error

以下在使用时有效System.Data.OracleClient- 但显然这是折旧的。使用完全相同的代码但使用时Oracle.DataAccess.OracleClient出现以下错误

{"ORA-12545: Connect failed because target host or object does not exist"}

Here is my code

这是我的代码

using (OracleConnection con = new OracleConnection("Data Source=orac;User Id=SYSTEM; Password=Pass;"))
        {
            con.Open();
            Console.WriteLine("Connection opened");

            OracleCommand cmd2 = new OracleCommand("SELECT * FROM SYSTEM.TABLE", con);
            OracleDataReader oracleDataReader = cmd2.ExecuteReader();

            while (oracleDataReader.Read())
            {
                Console.WriteLine(oracleDataReader[0]);
            }
        }

Could anyone help me out with what I am doing wrong?

谁能帮我解决我做错了什么?

I had to change the program to be 32 bit for the Oracle.DataAccess.dllto load.

我不得不将程序更改为 32 位Oracle.DataAccess.dll才能加载。

Is this even the correct library to be using???

这甚至是要使用的正确库吗???

Thanks

谢谢

EDIT

编辑

tnsnames.ora file as follows:

tnsnames.ora 文件如下:

# tnsnames.ora Network Configuration File: 
C:\app\UserName\product.2.0\dbhome_3\network\admin\tnsnames.ora
# Generated by Oracle configuration tools.

ORAC =
 (DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1522))
(CONNECT_DATA =
  (SERVER = DEDICATED)
  (SERVICE_NAME = orac.example.com)
)
 )

LISTENER_ORAC =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1522))

UPDATE

更新

I have tried connecting using Oracle Sql developer. This works so long as I use the TNS connection type, but doesn't work if I use the Basic connection type.

我尝试使用 Oracle Sql developer 进行连接。只要我使用 TNS 连接类型,这就会起作用,但如果我使用基本连接类型,则它不起作用。

Also I've noticed that in my services there are 2 called

我也注意到在我的服务中有 2 个叫

OracleOraDb11g_home1TNSListener OracleOraDb11g_home2TNSListener

OracleOraDb11g_home1TNSListener OracleOraDb11g_home2TNSListener

The orac dbhome is home3 I believe, could this be the issue?? If so does anyone know how I would go about fixing it?

我相信 orac dbhome 是 home3,这可能是问题吗??如果是这样,有谁知道我将如何修复它?

Thanks

谢谢

UPDATEIn HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\ there are two properties (Default) and inst_loc. There are a few folders two of which are called "KEY_OraDb11g_home1" and "KEY_OraDb11g_home2" these both contain a property called ORACLE_HOME they are:

UPDATE在 HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\ 中有两个属性(默认)和 inst_loc。有几个文件夹,其中两个名为“KEY_OraDb11g_home1”和“KEY_OraDb11g_home2”,它们都包含一个名为 ORACLE_HOME 的属性,它们是:

KEY_OraDb11g_home1 - ORACLE_HOME = C:\app\UserName\product\11.2.0\dbhome_2 KEY_OraDb11g_home2 - ORACLE_HOME = C:\app\UserName\product\11.2.0\dbhome_3

KEY_OraDb11g_home1 - ORACLE_HOME = C:\app\UserName\product\11.2.0\dbhome_2 KEY_OraDb11g_home2 - ORACLE_HOME = C:\app\UserName\product\11.2.0\dbhome_3

The SID for the Db in home1 is something called OracleDev, I may have installed this at some point long ago, but I certainly don't need it now if it is causing a problem

home1 中 Db 的 SID 称为 OracleDev,我可能很久以前就安装了它,但如果它引起问题,我现在当然不需要它

Thanks

谢谢

回答by Artless

EDIT

编辑

Let's try something else.

让我们试试别的。

It's possible that ODP.NET doesn't know where to find your Oracle Home because it is not defined in your PATH environment variable, or if it is not set in the registry.

ODP.NET 可能不知道在哪里可以找到您的 Oracle Home,因为它没有在您的PATH 环境变量中定义,或者如果它没有在注册表中设置。

Check your PATH and make sure it contains your Oracle Home directory, and "Oracle Home\bin". For the registry, look for HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\. In it there should be a key called HOME0with a string value of ORACLE_HOME. Make sure it points to your Oracle Home.

检查您的 PATH 并确保它包含您的 Oracle 主目录和“Oracle Home\bin”。对于注册表,查找HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\. 其中应该有一个键HOME0,其字符串值为ORACLE_HOME. 确保它指向您的 Oracle Home。

回答by teamaster

Check the host file, under C:\windows\system32\drivers\etc, especially if the machine is VM and the IP address of local host might change,if IP address of VM is not static. Change the IP of local host on that file if necessary

查看C:\windows\system32\drivers\etc下的host文件,特别是如果机器是VM,如果VM的IP地址不是静态的,本地主机的IP地址可能会发生变化。如有必要,更改该文件上本地主机的 IP

回答by Peter Chikov

Had a similar issue:
Visual Studio used the wrong version of tnsnames.ora in ..\11.2.0\client_1\network\admin
TNS Ping used the correct version of tnsnames.ora in ..\11.2.0\client_2\network\admin

有一个类似的问题:
Visual Studio 在 ..\11.2.0\client_1\network\admin 中
使用了错误版本的 tnsnames.ora TNS Ping 在 ..\11.2.0\client_2\network\ 中使用了正确版本的 tnsnames.ora行政

Hence tnsping was successful but Visual Studio returned: ORA-12545: Connect failed because target host or object does not exist.

因此 tnsping 成功,但 Visual Studio 返回:ORA-12545:连接失败,因为目标主机或对象不存在。

Solution, probably not the best one, copied the correct version of tnsnames.ora to ..\11.2.0\client_1\network\admin

解决方案,可能不是最好的,将正确版本的 tnsnames.ora 复制到 ..\11.2.0\client_1\network\admin

回答by Kishor Pawar

I don't know why but setting HOSTto 0.0.0.0in tnsnames.oraand listner.oraworked for me.

我不知道为什么,但设置HOST0.0.0.0tnsnames.oralistner.ora我的工作。

Of course, had to run lsnrctl start

当然得跑 lsnrctl start