连接字符串问题 Oracle .Net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6237976/
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
Connection String Problem Oracle .Net
提问by Nick LaMarca
I am new to oracle and am trying to simply connect to an oracle db, but I am not sure where to find the proper credentials to put in the connection string. I simply downloaded and install oracle express edition on my machine, then installed the .Net references. My simple code is here:
我是 oracle 的新手,正在尝试简单地连接到 oracle 数据库,但我不确定在哪里可以找到正确的凭据以放入连接字符串。我只是在我的机器上下载并安装了 oracle express 版本,然后安装了 .Net 引用。我的简单代码在这里:
string oradb = "Data Source=XE;User Id=hr;Password=hr;";
OracleConnection conn = new OracleConnection(oradb); // C#
try
{
conn.Open();
string sql = "SELECT FIRST_NAME FROM EMPLOYEES WHERE EMAIL='SKING'"; // C#
OracleCommand cmd = new OracleCommand(sql, conn);
cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader(); // C#
dr.Read();
//label1.Text = dr["dname"].ToString(); // C# retrieve by column name
label1.Text = dr.GetString(0).ToString(); // return a .NET data type
//label1.Text = dr.GetOracleString(0).ToString(); // return an Oracle data type
}
catch (OracleException ex)
{
label1.Text = ex.Message;
}
finally
{
conn.Close();
}
I am getting a TNS:could not resolve the connect identifier specified exception. Its probably because my connection string is wrong is what I am guessing. I cannot even go to the Server Explorer dialog in Visual Studio and test a connection correctly to my oracle db.
我收到 TNS: 无法解析连接标识符指定的异常。这可能是因为我的连接字符串是错误的,这是我的猜测。我什至无法转到 Visual Studio 中的服务器资源管理器对话框并正确测试与我的 oracle 数据库的连接。
What steps do I need to take to figure out the proper credentials to plug into my connection string? Or wording it like this.... If you were going to install oracle express on your machine, then connect to a .Net app what steps would you take to set up the connection string?
我需要采取哪些步骤来找出插入连接字符串的正确凭据?或者这样说.... 如果您要在您的机器上安装 oracle express,然后连接到 .Net 应用程序,您将采取哪些步骤来设置连接字符串?
采纳答案by Joost Evertse
Maybe it is looking for a data source defined in a tnsnames.ora file called XE.
也许它正在寻找在名为 XE 的 tnsnames.ora 文件中定义的数据源。
Try the Easy Connect naming method in the Express edition. It enables application clients to connect to a database without using any configuration files, simply by specifying the data source attribute through syntax shown below:
尝试 Express 版本中的 Easy Connect 命名方法。它使应用程序客户端无需使用任何配置文件即可连接到数据库,只需通过如下所示的语法指定数据源属性:
user id=hr;password=hr;data source=hr-server
user id=hr;password=hr;data source=hr-server:1521
user id=hr;password=hr;data source=hr-server:1521/XE
Replace hr-server with the dns name or ip of your machine.
将 hr-server 替换为您机器的 dns 名称或 ip。