使用ASP.NET将错误连接到远程Oracle XE数据库

时间:2010-05-03 15:46:14

标签: c# asp.net oracle oracle-xe

我已经在我的开发机器上安装了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 个答案:

答案 0 :(得分: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

启动此侦听器

[root@rce-dev2 ~]# 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)));";*

答案 1 :(得分:0)

您是否需要在连接字符串中连接服务名称?例如,

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