2010-05-03 119 views
0

我在我的開發機器上安裝了Oracle XE,並且工作正常。使用ASP.NET將錯誤連接到遠程Oracle XE數據庫

然後,我在我的測試機器上安裝了Oracle XE客戶端,這也正常工作,我可以從瀏覽器訪問開發PC數據庫。

現在,我想創建一個可以訪問Oracle XE數據庫的ASP.Net應用程序。 我也試過,但它總是在我的TEST機器上顯示錯誤,使用ASP.Net將數據庫連接到開發機器。

這裏是我的ASP.Net應用程序代碼:

protected void Page_Load(object sender, EventArgs e) 
     { 
      string connectionString = GetConnectionString(); 

      OracleConnection connection = new OracleConnection(connectionString); 
       connection.Open(); 
       Label1.Text = "State: " + connection.State; 
       Label1.Text = "ConnectionString: " + connection.ConnectionString; 

       OracleCommand command = connection.CreateCommand(); 
       string sql = "SELECT * FROM Users"; 
       command.CommandText = sql; 
       OracleDataReader reader = command.ExecuteReader(); 
       while (reader.Read()) 
       { 
        string myField = (string)reader["nID"]; 
        Console.WriteLine(myField); 
       } 

     } 

     static private string GetConnectionString() 
     { 
      // To avoid storing the connection string in your code, 
      // you can retrieve it from a configuration file. 
      return "User Id=System;Password=admin;Data Source=(DESCRIPTION=" + 
        "(ADDRESS=(PROTOCOL=TCP)(HOST=myServerAddress)(PORT=1521))" + 
        "(CONNECT_DATA=(SERVICE_NAME=)));"; 
     } 
+2

什麼錯誤,你恰好有?這可能是ORA-12541:TNS:沒有聽衆? – 2010-05-03 15:50:48

回答

0

不要你需要一個服務名連接到您的連接字符串中?例如,

return "User Id=System;Password=admin;Data Source=(DESCRIPTION=" + 
        "(ADDRESS=(PROTOCOL=TCP)(HOST=myServerAddress)(PORT=1521))" + 
        "(CONNECT_DATA=(SERVICE_NAME=myDBServiceName)));"; 
+0

我試過但仍然無法工作。 有關信息,我使用Windows Server 2008 - 64位和VS 2008.我的代碼在Windows 7 32位下正常工作。 – imsatasia 2010-05-03 19:47:08

+0

這將有助於瞭解確切的錯誤消息/代碼。 – DCookie 2010-05-03 19:54:52

+0

如果是XE,那麼它應該是SERVICE_NAME = XE – 2010-05-03 23:29:59

1

確保oracle監聽器已打開。

ps -ef | grep ls 
oracle 3773  1 0 May24 ?  00:00:00 /usr/local/oracle/product/10.2.0.1.0/bin/tnslsnr LISTENER -inherit 

啓動這個監聽

[[email protected] ~]# su - oracle 
-bash-3.2$ cd $ORACLE_HOME 
-bash-3.2$ cd bin 
-bash-3.2$ ls *ls* 
-bash-3.2$ lsnrctl 

LSNRCTL for Linux: Version 10.2.0.1.0 - Production on 25-MAY-2012 11:53:10 

Copyright (c) 1991, 2005, Oracle. All rights reserved. 

Welcome to LSNRCTL, type "help" for information. 

LSNRCTL> start 

Starting /usr/local/oracle/product/10.2.0.1.0/bin/tnslsnr: please wait... 

TNSLSNR for Linux: Version 10.2.0.1.0 - Production 

System parameter file is /usr/local/oracle/product/10.2.0.1.0/network/admin/listener.ora 

Log messages written to /usr/local/oracle/product/10.2.0.1.0/network/log/listener.log 

Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=rce-dev2.microlink.com.my)(PORT=1521))) 

Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC0))) 


Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=rce-dev2.microlink.com.my)(PORT=1521))) 

STATUS of the LISTENER 

------------------------ 

Alias      LISTENER 

Version     TNSLSNR for Linux: Version 10.2.0.1.0 - Production 

Start Date    25-MAY-2012 11:53:54 

Uptime     0 days 0 hr. 0 min. 0 sec 

Trace Level    off 

Security     ON: Local OS Authentication 

SNMP      OFF 

Listener Parameter File /usr/local/oracle/product/10.2.0.1.0/network/admin/listener.ora 

Listener Log File   /usr/local/oracle/product/10.2.0.1.0/network/log/listener.log 

Listening Endpoints Summary... 

    (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=rce-dev2.microlink.com.my)(PORT=1521))) 

    (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC0))) 

Services Summary... 

Service "PLSExtProc" has 1 instance(s). 

    Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service... 

The command completed successfully 

你可以試試這個

"User Id=System;Password=admin;Data Source=(DESCRIPTION=" + 
        "(ADDRESS=(PROTOCOL=TCP)(HOST=myServerAddress)(PORT=1521))" + 
        "(CONNECT_DATA=(SID=NETBDS)));";*