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

发布于 2024-08-31 07:12:18 字数 1486 浏览 4 评论 0原文

我已在我的开发计算机上安装了 Oracle XE,并且运行良好。

然后我在我的测试机上安装了 Oracle XE 客户端,它也运行良好,我可以从浏览器访问开发 PC 数据库。

现在,我想创建一个可以访问该 Oracle XE 数据库的 ASP.Net 应用程序。 我也尝试过,但在我的测试机器上使用 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=)));";
        }

I have installed Oracle XE on my Development machine and it is working fine.

Then I installed Oracle XE client on my Test machine which is also working fine and I can access Development PC database from Browser.

Now, I want to create an ASP.Net application which can access that Oracle XE database.
I tried it too, but it always shows me an error on my TEST machine to connect database to the Development Machine using ASP.Net.

Here is my code for ASP.Net application:

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=)));";
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘你却要生生世世 2024-09-07 07:12:18

确保 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)));";*

make sure the oracle listener opened.

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

to start this listener

[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

you can try this one

"User Id=System;Password=admin;Data Source=(DESCRIPTION=" + 
                    "(ADDRESS=(PROTOCOL=TCP)(HOST=myServerAddress)(PORT=1521))" + 
                    "(CONNECT_DATA=(SID=NETBDS)));";*
顾挽 2024-09-07 07:12:18

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

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

Don't you need a service name to connect to in your connection string? E.g.,

return "User Id=System;Password=admin;Data Source=(DESCRIPTION=" +
                    "(ADDRESS=(PROTOCOL=TCP)(HOST=myServerAddress)(PORT=1521))" +
                    "(CONNECT_DATA=(SERVICE_NAME=myDBServiceName)));";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文