出现此错误“ORA-06413:连接未打开”

发布于 2024-12-24 23:10:16 字数 1269 浏览 0 评论 0原文

我使用的是 Visual Studio 2010 和 Oracle 10g。操作系统:Windows 7 家庭基本版。如果我在 Visual Studio 2005 中使用以下代码,我将得到正确的输出。但在 Visual Studio 2010 中我收到错误

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Windows.Forms;

public partial class Default2 : System.Web.UI.Page
{
    OracleConnection con = new OracleConnection("uid=scott;pwd=tiger;data source=");
    OracleCommand cmd = new OracleCommand();
    OracleDataReader dr;
    string name1, pass1;
    protected void Page_Load(object sender, EventArgs e)
    {
        con.Open();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        cmd = new OracleCommand("Select* from tb1 where name='" + TextBox1.Text + "'and pass='"+TextBox2.Text + "'", con);
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            name1 = dr[0].ToString();
            pass1 = dr[1].ToString();
        }
        if (name1 == TextBox1.Text && pass1 == TextBox2.Text)
        {
            Response.Redirect("Home.aspx");
        }
        else
        {
            MessageBox.Show("Incorrect user name or password");
        }
    }
}

I am using visual studio 2010 and Oracle 10g. Operating system: Windows 7 home basic. If I use the following code in visual studio 2005, I am getting a correct output. But in Visual Studio 2010 I get an error

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Windows.Forms;

public partial class Default2 : System.Web.UI.Page
{
    OracleConnection con = new OracleConnection("uid=scott;pwd=tiger;data source=");
    OracleCommand cmd = new OracleCommand();
    OracleDataReader dr;
    string name1, pass1;
    protected void Page_Load(object sender, EventArgs e)
    {
        con.Open();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        cmd = new OracleCommand("Select* from tb1 where name='" + TextBox1.Text + "'and pass='"+TextBox2.Text + "'", con);
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            name1 = dr[0].ToString();
            pass1 = dr[1].ToString();
        }
        if (name1 == TextBox1.Text && pass1 == TextBox2.Text)
        {
            Response.Redirect("Home.aspx");
        }
        else
        {
            MessageBox.Show("Incorrect user name or password");
        }
    }
}

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

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

发布评论

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

评论(5

猥︴琐丶欲为 2024-12-31 23:10:16

这可能是定义连接的 tnsnames.ora 的问题。

您可以使用以下连接字符串省略 tnsnames.ora

OracleConnection con = new OracleConnection("Data Source=(DESCRIPTION =(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = !!!!!!!!YOURDBNAME!!!!!!!!!)(SERVER = DEDICATED))); User Id=Scott;Password=tiger;");

It could be a problem with the tnsnames.ora, where your connection is defined.

You can omit tnsnames.ora with this connection string:

OracleConnection con = new OracleConnection("Data Source=(DESCRIPTION =(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = !!!!!!!!YOURDBNAME!!!!!!!!!)(SERVER = DEDICATED))); User Id=Scott;Password=tiger;");
蛮可爱 2024-12-31 23:10:16

从页面加载中删除打开的连接并在执行之前添加以下内容:

if(con.State == ConnectionState.Closed)
{
        con.Open();
}

dr = cmd.ExecuteReader();

Remove the connection open from the page load and add the following before the execute:

if(con.State == ConnectionState.Closed)
{
        con.Open();
}

dr = cmd.ExecuteReader();
也只是曾经 2024-12-31 23:10:16

请在您的硬盘上搜索名为 tnsnames.ora 的文件,并在此处发布该文件的内容。

还有一些提示:

  • 每次页面加载时都会实例化并打开 con 对象。
  • 单击按钮也会强制页面加载和页面加载事件。请参阅“常规页面生命周期阶段”。
  • 将代码移至按钮单击处理程序。
  • MessageBox 始终显示在服务器上,客户端的用户永远不会看到此消息。当你开发的时候服务端和客户端是在同一台物理机上的。
  • default2.aspx 添加一个 div。

并调用:

error.InnerText="Incorrect user name or password";

检查名称和密码两次,一次在 SQL 语句中,一次在代码中。这是更好的:

dr.Read(); 
    If (dr.HasRows())
        Response.Redirect("Home.aspx");
    else
        error.InnerText="Incorrect user name or password";

Please search your hard drive for a file named tnsnames.ora, and post the contents of the file here.

And some tips:

  • The con object will instanciated and opened every time the page loads.
  • A button click forces a page load and a page load event too. See "General Page Life-Cycle Stages".
  • Move the code to the button-clíck Handler.
  • MessageBox is always shown on the server, a user from a client will never see this message. When you develop the server and client are on the same physical machine.
  • Add a div to your default2.aspx.

and call:

error.InnerText="Incorrect user name or password";

Check name and password twice, one time in the SQL statement, and one time in the code. This is better:

dr.Read(); 
    If (dr.HasRows())
        Response.Redirect("Home.aspx");
    else
        error.InnerText="Incorrect user name or password";
め七分饶幸 2024-12-31 23:10:16

您尚未将连接分配给该命令,在执行该命令之前您需要如下所示的内容:

cmd.Connection = con;

You have not assigned the connection to the command, you need something like below before you execute the command:

cmd.Connection = con;
因为看清所以看轻 2024-12-31 23:10:16

对比 2010 年:
我想分享一个经验,当时我通过 WCF 服务汇集 oracel 连接并在开发服务器上托管该 WCF。收到错误“ORA-06413:连接未打开”。找不到网络搜索的实际帮助。最后,我在生产服务器上部署了 WCF 的编译版本。当我从我的应用程序中使用 WCF 服务时,它对我有用......

希望这可以帮助面临同样问题的人......

VS 2010:
i would like to share anexperience, when i was pooling oracel connection via WCF service and hosting that WCF on development server. getting that error "ORA-06413: Connection not open". could not find actual help for web serach. Finally, i deployed the compiled version of WCF on production server. when i used that WCF service from my application it worked for me....

Hope this could help someone facing the same issue...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文