.net VS2010 + Oracle 驱动程序:ORA-12154:TSN:无法解析指定的连接标识符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4228739/
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
VS2010 + Oracle driver: ORA-12154: TSN:could not resolve the connect identifier specified
提问by Matt
I am using:
我在用:
- Visual Studio 2010
- .Net Framework Data Provider for Oracle
- Oracle Developer Tools for Visual Studio (from Oracle's website)
- 视觉工作室 2010
- 用于 Oracle 的 .Net Framework 数据提供程序
- 适用于 Visual Studio 的 Oracle 开发人员工具(来自 Oracle 网站)
I tried installing 'Oracle Developer Tools for Visual Studio' and created tnsnames.ora and sqlnet.ora files in my C:\app\ [my username]\product\11.2.0\client_1\Network\Admin directory.
我尝试安装“Oracle Developer Tools for Visual Studio”并在我的 C:\app\ [我的用户名]\product\11.2.0\client_1\Network\Admin 目录中创建了 tnsnames.ora 和 sqlnet.ora 文件。
They look like this:
它们看起来像这样:
# tnsnames.ora
ORATEST =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = dbs-oratest)(PORT = 1521))
)
(CONNECT_DATA =
(SID = [ORATEST])
)
)
and
和
# sqlnet.ora
SQLNET.AUTHENTICATION_SERVICES= (ALL)
NAMES.DIRECTORY_PATH= (TNSNAMES, EZCONNECT)
When I try using the .Net Framework Data Provider for Oracle driver to set up a new connection (or any other driver for that matter: OLE, ODBC, etc) it gives the error:
当我尝试使用 .Net Framework Data Provider for Oracle 驱动程序来设置新连接(或任何其他驱动程序:OLE、ODBC 等)时,它给出了错误:
ORA-12154: TSN:could not resolve the connect identifier specified
ORA-12154: TSN: 无法解析指定的连接标识符
Using the OLE driver on a machine withoutthe Oracle client installed DOES work though:
在没有安装 Oracle 客户端的机器上使用 OLE 驱动程序确实可以工作:
OleDbConnection conn = new OleDbConnection(
"Provider=MSDAORA;Data Source=ORATEST;" +
"Persist Security Info=True;Password=readonly;User ID=readonlyuser");
What am I doing wrong? Are there any simple instructions online about how to install a basic Oracle driver?
我究竟做错了什么?在线是否有关于如何安装基本 Oracle 驱动程序的简单说明?
Thank you in advance!
先感谢您!
回答by Matt
The best solution I found was to use the Oracle Data Access Client library, and include the entire TNS names entry in the connection string. This allows the project to be easily published to a web server, ClickOnce, etc.
我找到的最佳解决方案是使用 Oracle 数据访问客户端库,并在连接字符串中包含整个 TNS 名称条目。这允许将项目轻松发布到 Web 服务器、ClickOnce 等。
Here are the steps necessary to set up the Oracle driver working in your project:
以下是设置在您的项目中运行的 Oracle 驱动程序所需的步骤:
1) Get DLLs from 'Oracle Data Provider for .NET' package
1) 从“Oracle Data Provider for .NET”包中获取 DLL
Download installer file from this location: http://www.oracle.com/technetwork/topics/dotnet/index-085163.html
从以下位置下载安装程序文件:http: //www.oracle.com/technetwork/topics/dotnet/index-085163.html
I went ahead and installed the full 200 MB ODAC with Oracle Developer Tools for Visual Studio, but you only really need four DLLs from this download. (You may be able to extract them directly from the installer package, instead of going through the entire install process, or perhaps one of the smaller download includes all of them.)
我继续使用适用于 Visual Studio 的 Oracle 开发人员工具安装了完整的 200 MB ODAC,但您实际上只需要从该下载中获得四个 DLL。(您可以直接从安装程序包中提取它们,而不是经历整个安装过程,或者较小的下载之一可能包含所有这些。)
2) Reference DLLs in your project
2) 在您的项目中引用 DLL
Search the installation directory of the Oracle Data Access Client and drag the following four DLLs into the root of your project:
搜索 Oracle Data Access Client 的安装目录,将以下四个 DLL 拖到您的项目根目录中:
- Oracle.DataAccess.dll
- oci.dll
- oraciicus11.dll
- OraOps11w.dll
- Oracle.DataAccess.dll
- oci.dll
- oracicicus11.dll
- OraOps11w.dll
Set the Copy to Output Directoryproperty all of the files except Oracle.DataAccess.dll to Copy always.
将除 Oracle.DataAccess.dll 之外的所有文件的复制到输出目录属性设置为始终复制。
Under Project--> Add Reference..., click on the Browsetab and select the Oracle.DataAccess.dll file.
在Project--> Add Reference... 下,单击Browse选项卡并选择 Oracle.DataAccess.dll 文件。
3) Use the driver with full connection string (optional)
3) 使用带有完整连接字符串的驱动程序(可选)
So as not to have to worry about TNS names files being set up on the machines the application was deployed to, I put the entire definition in the file as shown by connectionstrings.com. It makes the connection string a little bulky, but removed a lot of the TNS Names file headaches I was experiencing before:
为了不必担心在应用程序部署到的机器上设置 TNS 名称文件,我将整个定义放在文件中,如connectionstrings.com所示。它使连接字符串有点笨重,但消除了我之前遇到的许多 TNS Names 文件问题:
Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=servername)(PORT=??1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=servicename)));User Id=username;Password=********;
Here's the full class I used to test the driver:
这是我用来测试驱动程序的完整课程:
using System;
using System.Data;
using Oracle.DataAccess.Client;
static class Program
{
[STAThread]
static void Main()
{
TestOracle();
}
private static void TestOracle()
{
string connString =
"Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)" +
"(HOST=servername)(PORT=??1521)))" +
"(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=servicename)));"+
"User Id=username;Password=********;";
using (OracleConnection conn = new OracleConnection(connString))
{
string sqlSelect = "SELECT * FROM TEST_TABLE";
using (OracleDataAdapter da = new OracleDataAdapter(sqlSelect, conn))
{
var table = new DataTable();
da.Fill(table);
if (table.Rows.Count > 1)
Console.WriteLine("Successfully read oracle.");
}
}
}
}
回答by Eric Schneider
You should use the Oracle Data Access Client library, and then the OracleConnection object instead.
您应该使用 Oracle 数据访问客户端库,然后改用 OracleConnection 对象。
http://www.oracle.com/technology/sample_code/tech/windows/odpnet/howto/connect/index.html
http://www.oracle.com/technology/sample_code/tech/windows/odpnet/howto/connect/index.html
I know that Oracle is kind of picky with TNS names file. I usually count on the DBA's for this. SQL-Server is much easier to get going...
我知道 Oracle 对 TNS 名称文件有点挑剔。为此,我通常指望 DBA。SQL-Server 更容易上手...
回答by Mike Dole
Use local IIS web server instead of Visual Studio Development server (Project Settings - WEB) did the trick for me!
使用本地 IIS Web 服务器而不是 Visual Studio 开发服务器(项目设置 - WEB)对我有用!
Tns-12154 had me pulling my hair out... web site worked fine in VS2008...
Tns-12154 让我拔头发...网站在 VS2008 中运行良好...
Regards,
问候,
Mike
麦克风
回答by Fernando
The code that I′m using is the above.
我正在使用的代码是上面的代码。
P.S.: I′ve tested many times. Using Visual Studio .Net 2010 (VB.Net 2010).
PS:我已经测试过很多次了。使用 Visual Studio .Net 2010 (VB.Net 2010)。
Dim conn As New Odbc.OdbcConnection
Dim cmd As New Odbc.OdbcCommand
Dim drResult As Odbc.OdbcDataReader
Dim connString As String
Dim QuerySQL As String
connString = "Driver={Microsoft ODBC for Oracle};CONNECTSTRING=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=ORACLEDB01)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORACLE_INSTANCE_NAME)));Uid=john;Pwd=mypassword;"
QuerySQL = "select first_name, last_name from employees where id = 28"
conn.ConnectionString = connString
conn.Open()
cmd.Connection = conn
cmd.CommandText = QuerySQL
drResult = cmd.ExecuteReader()
While drResult.Read
TextBox1.Text = TextBox1.Text & drResult("last_name") & ", " & drResult("first_name") & Environment.Newline
End While
drResult.Close()

