我正在尝试连接到远程Oracle服务器。我的连接字符串 -
OdbcConnection con = new OdbcConnection();
con.ConnectionString = @"Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST= xxxx)(PORT=xxxxx))(CONNECT_DATA=(SERVER=dedicated)(SERVICE_NAME=abc.domain.com)));USER ID=user1;Password=pwd;";
我遇到错误 - “ERROR [IM002] [Microsoft] [ODBC驱动程序管理器]未找到数据源名称且未指定默认驱动程序”(System.Data.Odbc.OdbcException)异常消息=“ERROR [IM002] [ Microsoft] [ODBC驱动程序管理器] 未找到数据源名称且未指定默认驱动程序“,Exception Type =”System.Data.Odbc.OdbcException“,Exception WinRT Data =”“
我根据我的TNSNAMES.ora
指定了我的连接字符串我在TNSNAMES.ora中的数据库条目如下:
DB.WORLD=
(DESCRIPTION=
(ADDRESS=
(PROTOCOL=TCP)
(HOST= xxxx)
(PORT=xxxxx)
)
(CONNECT_DATA=
(SERVER=dedicated)
(SERVICE_NAME=abc.domain.com)
)
)
有人可以解释错误。如果我的连接字符串出错以及如何从我的Windows应用程序连接到Oracle服务器,请帮助/建议
答案 0 :(得分:4)
首先使用nuget packet manager安装odp.net.managed:
Install-Package odp.net.managed
没有预装Oracle客户端的odp.net.managed工作
下:
const string connectionString = @"Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST= xxxx)(PORT=xxxxx))(CONNECT_DATA=(SERVER=dedicated)(SERVICE_NAME=abc.domain.com)));USER ID=user1;Password=pwd;";
var connection = new OracleConnection(connectionString);
connection.Open();
如果你在应用程序文件夹中有tnsnames.ora:
const string connectionString = @"Data Source=DB.WORLD;USER ID=user1;Password=pwd;";
var connection = new OracleConnection(connectionString);
connection.Open();
或者如果是其他文件夹中的tnsnames.ora:
Environment.SetEnvironmentVariable("TNS_ADMIN", @"path_to_tnsadmin.ora");
const string connectionString = @"Data Source=DB.WORLD;USER ID=user1;Password=pwd;";
var connection = new OracleConnection(connectionString);
connection.Open();
答案 1 :(得分:2)
您需要使用OracleConnection
OracleConnection conn = new OracleConnection(connectionString);
答案 2 :(得分:0)
转到Connections Strings for Oracle
也许会找到一些帮助
答案 3 :(得分:0)
使用以下代码:
using System;
using Oracle.DataAccess.Client;
class ConnectionSample
{
static void Main()
{
OracleConnection con = new OracleConnection();
//using connection string attributes to connect to Oracle Database
con.ConnectionString = "User Id=scott;Password=tiger;Data Source=oracle";
con.Open();
Console.WriteLine("Connected to Oracle" + con.ServerVersion);
// Close and Dispose OracleConnection object
con.Close();
con.Dispose();
Console.WriteLine("Disconnected");
}
}
答案 4 :(得分:0)
尝试类似这样的课程:
public class OracleOperations
{
OracleConnection oraConn = new OracleConnection();
private bool connStatus;
public OracleOperations()
{
connStatus = false;
connect();
}
~OracleOperations()
{
disconnect();
}
public bool getConnStatus()
{
return connStatus;
}
public void connect()
{
string connString = "User Id=xxxx; Password=yyyyy; Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.10.10.10)(PORT=1583))(CONNECT_DATA=(SERVER=dedicated)(SID=oracledb)))";
if (oraConn.State != ConnectionState.Open)
{
try
{
oraConn.ConnectionString = connString;
oraConn.Open();
Console.WriteLine("Successful Connection");
connStatus = true;
}
catch (Exception eOra)
{
Console.WriteLine(eOra.Message+ "Exception Caught");
connStatus = false;
throw eOra;
}
}
}
public void disconnect()
{
if (oraConn.State == ConnectionState.Open)
{
try
{
oraConn.Close();
connStatus = false;
Console.WriteLine("Connection Closed");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + "Exception Caught");
}
}
}
}
答案 5 :(得分:0)
我会尝试使用Tnsping实用程序来确保您可以通过tnsnames.ora连接
尝试将tnsnames.ora和sqlnet.ora放在应用程序的同一文件夹中,看看是否解决了这个问题。
使用Managed ODP.Net,有一个不支持LDAP查找的问题(例如LDAP.ora)
答案 6 :(得分:-1)
我' Ve创建了一个app.config文件并配置了这样的数据库条目
<configuration>
<configSections>
<section name ="Environment" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<Environment>
<add key ="CIT" value ="Password=pwd123;User ID=abc123;Data Source=db1;Persist Security Info=True;Provider=MSDAORA"/>
<add key ="SIT" value ="Password=pwd234;User ID=abc234;Data Source=db2;Persist Security Info=True;Provider=MSDAORA"/>
<add key ="UAT" value ="Password=pwd345;User ID=abc345;Data Source=db3;Persist Security Info=True;Provider=MSDAORA"/>
</Environment>
</configuration>
使用 ConfigurationManager 将该配置缓存到我的表单中(需要参考assembly - system.configuration)。添加命名空间 - 使用System.Collections 。专门用于获取 NameValueCollection 。代码就像这样
environments = ConfigurationManager.GetSection("Environment") as NameValueCollection;
string strConnString = environments[envs];
conn = new OleDbConnection(strConnString);
conn.Open();
OleDbDataAdapter objDa = new OleDbDataAdapter("select * from tblABC", conn);
DataSet ds1 = new DataSet();
objDa.Fill(ds1); dataGridView1.DataSource = ds1.Tables[0];
使用datset,我使用OleDbDataAdapter填充了数据网格。它适用于我的Windows应用程序。