c#中oracle连接问题
我有一台 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能想尝试以下操作:
连接字符串:
现在查看 PC 上的 tnsname.ora 文件。它将位于本地 Oracle 客户端安装下的某个位置。您正在寻找类似的内容:
您希望将
DBNAME
和OracleServerName
替换为您自己的。不要忘记,DBNAME
是主数据库的名称,而不是其下的模式之一(具有所有表和过程等的实体)。You might want to try something like:
connection string:
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:
You're looking to replace
DBNAME
andOracleServerName
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.假设您使用的是 ODP.Net,您可以尝试连接如下所示的字符串(取自
web.config
):您可以使用以下内容从代码中的 web.config 读取此连接字符串:
避免直接放置连接字符串是一个好习惯在代码中,因为更改它们需要重新编译。通过将它们放入配置文件(例如
web.config
或app.config
)中,您可以更改它们,而无需重新编译库或 exe。Assuming you are using ODP.Net, you can try a connection string such as the following (taken from a
web.config
):You can read this connection string from the web.config in your code using something like this:
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
orapp.config
, you can change them without having to re-compile your library or exe.这里有潜在的问题:如果 conn.Open() 失败(因此连接未打开),最后您调用 conn.close(),但连接未打开 - 所以它也失败,但它现在在 try catch 之外。您需要检查
oracle 的连接字符串:
数据源=TORCL;用户ID=myUsername;密码=myPassword;
数据源=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID)));用户ID=myUsername;密码=我的密码;
更多信息
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
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