OracleConnection.Open正在抛出ORA-12541 TNS没有监听器

时间:2012-09-24 19:16:22

标签: c# oracle oracle11g database-connection connection-string

所以我通过C#连接到外部服务器。我刚刚在我的机器上安装了 Oracle 11g 客户端:http://www.oracle.com/technetwork/database/windows/downloads/index-090165.html(每个255MB)。

在阅读了很多博客/问题后,我发现这篇文章很有用:

http://dbaspot.com/oracle-faq/444787-ora-12541-tns-no-listener.html

这是正确的吗?我什么都做不了。 DBA必须编辑 LISTENER.ORA 文件吗?

我的 tnsnames.ora 如下所示:

  TestingConnect=
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = TestHostName.us.local)(PORT = 1523))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = TEST)
    )
  )

它让我犯了错误:

Oracle.DataAccess.Client.OracleConnection connection = new Oracle.DataAccess.Client.OracleConnection();

connection.ConnectionString = "Data Source=TestHostName.us.local;Persist Security Info=True;" + "User ID=tesName;Password=test";

connection.Open() //Throwing ERR!!!

我该怎么办?我感谢任何评论。谢谢!

2 个答案:

答案 0 :(得分:9)

您可以通过以下几种方式执行此操作: 使用您的TNSNames文件,数据源应指定TNSHosts条目名称(tnsnames.ora中第一个“=”之前的位),而不是主机名:

connection.ConnectionString = "Data Source=TestingConnect;Persist Security Info=True;" + "User ID=tesName;Password=test"; 

或者您可以将整个TNS条目放在连接字符串中,如下所示:

connection.ConnectionString = "Data Source=(DESCRIPTION = " +
    "(ADDRESS = (PROTOCOL = TCP)(HOST = TestHostName.us.local)(PORT = 1523))" +
    "(CONNECT_DATA =" + 
    "(SERVER = DEDICATED)" + 
    "(SERVICE_NAME = TEST))" + 
    ");Persist Security Info=True;User ID=tesName;Password=test"; 

答案 1 :(得分:7)

感谢您的所有投入。我改变了我的连接字符串,它工作正常。这看起来像:

 private static string GetConnectionString()
    {
        return "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=TestHostName.us.local)(PORT=1523) ) )" +
               "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=CCDB)));User id=UserName; Password=Password; enlist=false; pooling=false;";
    }