Cannot Connect to Oracle in C#
I am trying to connect to Oracle in a 32-bit Console Application. I am getting the following erorr. The code (with the exception of host, username, and password change) is below. It is a simple two function project.
Any help will be appreciated.
I am using C# in Visual Studion 2010 Premium and Oracle 10g. I can connect to the database with Oracle SQL Developer with the information set in the connection string.
---------------ToString-------------------------- --Oracle.DataAccess.Client.OracleException at Oracle.DataAccess.Client.OracleException.HandleErrorHelper(Int32 errCode, OracleConnection conn, IntPtr opsErrCtx, OpoSqlValCtx* pOpoSqlValCtx, Object src, String procedure) at Oracle.DataAccess.Client.OracleException.HandleError(Int32 errCode, Oracle Connection conn, IntPtr opsErrCtx, Object src) at Oracle.DataAccess.Client.OracleConnection.Open() at ConsoleApplication1.Program.GetConnection() in c:usersmaholtdocumentsvisual studio 2010ProjectsConsoleApplication1ConsoleApplication1Program.cs:line 61
---------------Message---------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Oracle.DataAccess.Client;
namespace ConsoleApplication1
{
class Program
{
static OracleConnection conn;
static void Main(string[] args)
{
OracleConnection connC = GetConnection();
conn = connC;
simpleQuery();
Console.WriteLine("DONE");
}
public static void simpleQuery()
{
OracleCommand cmd = new OracleCommand("select count(*) as total from console.client");
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
try
{
cmd.Connection.Open();
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(Convert.ToString(reader["total"]));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
cmd.Dispose();
}
}
public static OracleConnection GetConnection()
{
Oracle.DataAccess.Client.OracleConnection rtn = null;
try
{
string connstr = "Data Source=//10.10.10.10:1521/PRD2_OLTP;User Id=user; Password=pass;";
rtn = new Oracle.DataAccess.Client.OracleConnection(connstr);
if (rtn.State != System.Data.ConnectionState.Open)
{
rtn.Open();
}
}
catch (Exception ee)
{
Console.WriteLine("-------------------------------------------------");
Console.WriteLine("---------------ToString--------------------------");
Console.WriteLine("--" + ee.ToString());
Console.WriteLine("---------------Message---------------------------");
Console.WriteLine("--" + ee.Message);
Console.WriteLine("-------------------------------------------------");
}
return (rtn);
}
}
}
SQL Developer uses effectively JDBC connection... so it is not really comparable with what happens in .NET :-(
Regarding the Oracle versus .NET version compatibility - I found this rather problematic esp. since the clients don't have always the option to update according to Oracle roadmap...
After researching some I switched to using the Devart components - support everything from Oracle 7.3 up to 11gR2 in .NET 2 up with 32 and 64 bit and come with a "direct-mode option" which means if need be I can run my app without any Oracle client being installed on the machine... not affiliated, just a happy customer...
First - Oracle doesn't support 10g with .net 4.0. You must use 11.2.0.2 or higher to be compliant with Oracle's supported versions.
Second - The problem is you probably don't have ODP.Net installed correctly. This can mean it isn't installed, it was installed to a second instance, or it failed to copy on or more files during installation.
I have some blog posts about these items along with a link to some connection testing applications I wrote. Feel free to use them.
https://tsells.wordpress.com/category/oracle/
链接地址: http://www.djcxy.com/p/67182.html下一篇: 无法在C#中连接到Oracle