我试图通过ASP.NET/C#连接到我的DB 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在同一家服务器上。是Oracle 11G。

当我进行TNSPAING时,我明白了:

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)

这是我的听众。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))
    )
  )
有帮助吗?

解决方案

将字符串连接放在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;

其他提示

尝试使用TNSPing诊断您的问题。这是在您的Oracle Home/bin目录中找到的实用程序。

http://www.orafaq.com/wiki/tnsping

EZCONNECT适配器是什么?也许tnsping和您的代码正在使用不同的驱动程序?

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

当我尝试将myComputer.mydomain用作我的连接字符串中的数据源时,它给出了错误代码

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

idk是什么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连接DB时,它可以正常工作...

非常感谢你的帮助。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top