oracle c#中oracle连接的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6030639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
problem with oracle connection in c#
提问by user757321
I have a Windows XP machine used to create .Net applications with VS 2008.
我有一台 Windows XP 机器,用于使用 VS 2008 创建 .Net 应用程序。
I want to connect to a remote network server (using Windows xp) that is running an Oracle 10g database.
我想连接到运行 Oracle 10g 数据库的远程网络服务器(使用 Windows xp)。
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.
我使用下面的代码(使用第一个连接字符串)直接连接到在同一台机器上运行的 10g 版本,没有问题,但是当我尝试连接到网络机器时,它实际上使我的应用程序崩溃。
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.
我担心的是我在应用程序中有双重 try/catch 语句,我不明白为什么它根本不拒绝连接并报告错误。
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();
}
回答by dcp
Assuming you are using ODP.Net, you can try a connection string such as the following (taken from a web.config
):
假设您使用的是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" />
You can read this connection string from the web.config in your code using something like this:
您可以使用以下内容从代码中的 web.config 读取此连接字符串:
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.
避免将连接字符串直接放在代码中是一个很好的做法,因为更改它们需要重新编译。通过将它们放在配置文件中,例如web.config
或app.config
,您可以更改它们而无需重新编译您的库或 exe。
回答by James Wiseman
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:
现在查看 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)
)
)
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.
您正在寻找替换DBNAME
和OracleServerName
用您自己的。不要忘记,DBNAME
是主数据库的名称,而不是其下的模式(具有所有表和过程等的实体)之一。
回答by VikciaR
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
你在这里有潜在的问题:如果 conn.Open() 失败(所以连接没有打开)最后你调用 conn.close(),但连接没有打开 - 所以它也失败了,但它现在在 try catch 之外。你需要检查
if (Conn!=null && Conn.IsOpen())
Conn.Close();
Connection string for oracle: Data Source=TORCL;User Id=myUsername;Password=myPassword;
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;
数据源=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID)));User Id=myUsername;密码=我的密码;