c#中oracle连接问题

发布于 2024-11-07 22:13:56 字数 1135 浏览 1 评论 0原文

我有一台 Windows XP 计算机,用于使用 VS 2008 创建 .Net 应用程序。

我想连接到运行 Oracle 10g 数据库的远程网络服务器(使用 Windows xp)。

我使用下面的代码(带有第一个连接字符串)直接连接到在同一台计算机上运行的 10g 版本,没有任何问题,但是当我尝试连接到网络计算机时,它实际上使我的应用程序崩溃了。

我尝试了连接字符串的几种变体,因为我觉得我一定在某个地方犯了语法错误。

我担心的是,我在应用程序中有双重 try/catch 语句,我不明白为什么它根本不拒绝连接并报告错误。

我想真正的问题是“连接字符串的正确语法是什么”……或者我到底做错了什么。

非常感谢任何帮助或建议。先感谢您。

//Class Variables
string CONNSTR = "Server=192.168.0.1:1521;User ID=zahid;Password=abc123;";


public Oracle()
{
  InitializeComponent();
}

//Methods
private void TestMyOracleConnection()
{
  OracleConnection Conn = new OracleConnection(CONNSTR);
  try
  {
    Conn.Open();
    MessageBox.Show("Oracle Connection Established", "Success");
  }
  catch (OracleException ex)
  {
    MessageBox.Show(ex.Message, "Oracle Connection Failed!");
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.Message, "Oracle Connection Failed!");
  }
  finally
  {
    Conn.Close();
    MessageBox.Show("Oracle Connection Closed", "Success");
  }
}

private void buttonTestConnection_Click(object sender, EventArgs e)
{
  TestMyOracleConnection();
}

I have a Windows XP machine used to create .Net applications with VS 2008.

I want to connect to a remote network server (using Windows xp) that is running an Oracle 10g database.

I am using the code below (with the first connection string) to connect directly to a version of 10g that is running on the same machine with no problems, however when I try to connect to the network machine, it actually crashes my application.

I have tried several variations of connection strings as I feel that I must be making a syntax error somewhere.

What concerns me is that I have dual try/catch statements in the application and I do not understand why it simply does not refuse the connection and report the error.

I suppose the real question is 'what is the correct syntax for the connection string'....or WHATEVER the hell I am doing wrong.

Any help or suggestions are greatly appreciated. Thank you in advance.

//Class Variables
string CONNSTR = "Server=192.168.0.1:1521;User ID=zahid;Password=abc123;";


public Oracle()
{
  InitializeComponent();
}

//Methods
private void TestMyOracleConnection()
{
  OracleConnection Conn = new OracleConnection(CONNSTR);
  try
  {
    Conn.Open();
    MessageBox.Show("Oracle Connection Established", "Success");
  }
  catch (OracleException ex)
  {
    MessageBox.Show(ex.Message, "Oracle Connection Failed!");
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.Message, "Oracle Connection Failed!");
  }
  finally
  {
    Conn.Close();
    MessageBox.Show("Oracle Connection Closed", "Success");
  }
}

private void buttonTestConnection_Click(object sender, EventArgs e)
{
  TestMyOracleConnection();
}

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

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

发布评论

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

评论(3

余生再见 2024-11-14 22:13:56

您可能想尝试以下操作:

连接字符串:

Data Source=DBNAME;User ID=zahid;Password= abc123;Persist Security Info=True;Unicode=True;

现在查看 PC 上的 tnsname.ora 文件。它将位于本地 Oracle 客户端安装下的某个位置。您正在寻找类似的内容:

DBNAME =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = OracleServerName)(PORT = 1510))
      (ADDRESS = (PROTOCOL = TCP)(HOST = OracleServerName)(PORT = 1514))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = DBNAME)
    )
  )

您希望将 DBNAMEOracleServerName 替换为您自己的。不要忘记,DBNAME 是主数据库的名称,而不是其下的模式之一(具有所有表和过程等的实体)。

You might want to try something like:

connection string:

Data Source=DBNAME;User ID=zahid;Password= abc123;Persist Security Info=True;Unicode=True;

Now look in the tnsname.ora file on your PC. It will be somewhere under your local Oracle Client installation. You're looking for something like:

DBNAME =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = OracleServerName)(PORT = 1510))
      (ADDRESS = (PROTOCOL = TCP)(HOST = OracleServerName)(PORT = 1514))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = DBNAME)
    )
  )

You're looking to replace DBNAME and OracleServerName with your own. Don't forget, DBNAME is the name of the main database, not one of the schemas (the entity with all the tables and procedures, etc) under it.

饮湿 2024-11-14 22:13:56

假设您使用的是 ODP.Net,您可以尝试连接如下所示的字符串(取自 web.config):

<add name="OdpConnection" connectionString="Data Source=xe;User Id=some_user;Password=some_password; Promotable Transaction=local"
     providerName="Oracle.DataAccess.Client" />

您可以使用以下内容从代码中的 web.config 读取此连接字符串:

string connectionString = ConfigurationManager.ConnectionStrings["OdpConnection"].ConnectionString;

避免直接放置连接字符串是一个好习惯在代码中,因为更改它们需要重新编译。通过将它们放入配置文件(例如 web.configapp.config)中,您可以更改它们,而无需重新编译库或 exe。

Assuming you are using ODP.Net, you can try a connection string such as the following (taken from a web.config):

<add name="OdpConnection" connectionString="Data Source=xe;User Id=some_user;Password=some_password; Promotable Transaction=local"
     providerName="Oracle.DataAccess.Client" />

You can read this connection string from the web.config in your code using something like this:

string connectionString = ConfigurationManager.ConnectionStrings["OdpConnection"].ConnectionString;

It's a good practice to avoid putting connection strings directly in code, since to change them requires a re-compile. By putting them in a config file, such as web.config or app.config, you can change them without having to re-compile your library or exe.

﹉夏雨初晴づ 2024-11-14 22:13:56
try
  {
    Conn.Open();
    MessageBox.Show("Oracle Connection Established", "Success");
  }
  catch (OracleException ex)
  {
    MessageBox.Show(ex.Message, "Oracle Connection Failed!");
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.Message, "Oracle Connection Failed!");
  }
  finally
  {
    Conn.Close();
    MessageBox.Show("Oracle Connection Closed", "Success");
  }

这里有潜在的问题:如果 conn.Open() 失败(因此连接未打开),最后您调用 conn.close(),但连接未打开 - 所以它也失败,但它现在在 try catch 之外。您需要检查

if (Conn!=null && Conn.IsOpen())
  Conn.Close();

oracle 的连接字符串:
数据源=TORCL;用户ID=myUsername;密码=myPassword;

数据源=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID)));用户ID=myUsername;密码=我的密码;

更多信息

try
  {
    Conn.Open();
    MessageBox.Show("Oracle Connection Established", "Success");
  }
  catch (OracleException ex)
  {
    MessageBox.Show(ex.Message, "Oracle Connection Failed!");
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.Message, "Oracle Connection Failed!");
  }
  finally
  {
    Conn.Close();
    MessageBox.Show("Oracle Connection Closed", "Success");
  }

You have pottential problem here: if conn.Open() fails (so connection is not opened) in finally you call conn.close(), but connection isn't opened - so it also fails, but it is now outside try catch. You need check for

if (Conn!=null && Conn.IsOpen())
  Conn.Close();

Connection string for oracle:
Data Source=TORCL;User Id=myUsername;Password=myPassword;

Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID)));User Id=myUsername;Password=myPassword;

More here

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