0

我正在尝试使用 ASP.NET/C# 连接到我的数据库 oracle。

这是我的代码:

OracleConnection connection = new OracleConnection();
connection.ConnectionString = @"Data Source=ORACLEDB;User id=me;Password=xxxx;";
try
{
   connection.Open();
}
catch (Exception ex)
{
    Console.WriteLine("Exception occurs when connecting to DB : " + ex.Message + ex.StackTrace);
}

这是我得到的错误:

ORA-12154: TNS : the identifier could not be solved

这是我的 TNSNAMES.ora:

ORACLEDB =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = myComputer.myDomain)(PORT = 1521))
    (CONNECT_DATA =
      (SERVICE_NAME = ORACLEDB)
    )
  )

我在win2k3服务器R2上工作。

Oracle 在同一台服务器上。它是甲骨文 11g。

当我做一个 tnsping 时,我得到了这个:

EZCONNECT adapter used to resolved the alias
Tentative de contact de (DESCRIPTION=(CONNECT_DATA=(SERVICE_NAME=))(ADDRESS=(PROTOCOL=TCP)(HOST=169.254.216.123)(PORT=1521)))
OK (20 msec)

这是我的 listener.ora:

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = CLRExtProc)
      (ORACLE_HOME = C:\app\Administrator\product\11.2.0\dbhome_1)
      (PROGRAM = extproc)
      (ENVS = "EXTPROC_DLLS=ONLY:C:\app\Administrator\product\11.2.0\dbhome_1\bin\oraclr11.dll")
    )
  )

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
      (ADDRESS = (PROTOCOL = TCP)(HOST = myComputer.myDomain)(PORT = 1521))
    )
  )
4

4 回答 4

4

将字符串连接放在 web.config 中:

  <connectionStrings>
    <add name="OracleDatabase"
          connectionString="Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME= ORACLEDB)));User Id= me;Password= xxx;Persist Security Info=True;"
          providerName="System.Data.OracleClient" />
  </connectionStrings>

然后从 web.config 中获取连接字符串:

connection.ConnectionString = ConfigurationManager.ConnectionStrings["OracleDatabase"].ConnectionString;
于 2011-01-05T10:21:31.410 回答
0

尝试使用 TNSPING 来诊断您的问题。它是您的 oracle home/bin 目录中的一个实用程序。

http://www.orafaq.com/wiki/Tnspi​​ng

于 2011-01-04T16:13:58.297 回答
0

什么是 EZConnect 适配器?也许 TNSPing 和您的代码使用不同的驱动程序?

尝试在代码的连接字符串中使用数据库的 IP 地址。这可能会向您展示导致问题的原因。

于 2011-01-04T16:14:37.080 回答
0

当我尝试在我的连接字符串中使用 myComputer.myDomain 作为数据源时,它会给出错误代码

ORA-12504 : TNS : the listener processus didn't obtain SERVICE_NAME in CONNECT_DATA.

我知道 EZConnect 是什么 :-)

命令 tnsping localhost 给出:

C:\app\Administrator\product\11.2.0\dbhome_1\network\admin\sqlnet.ora


Adaptateur TNSNAMES utilisé pour la résolution de l'alias
Tentative de contact de (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = myComputer.myDomain)(PORT = 1521)) (CONNECT_DATA = (SERVICE_NAME = ORACLEDB)))
OK (20 msec)

也许它可以帮助:当我尝试使用 SQL Plus 连接我的数据库时,它工作正常......

非常感谢你的帮助。

于 2011-01-04T17:19:14.103 回答