Click to See Complete Forum and Search --> : Unreliable connection to Oracle from C#


mpultz
July 12th, 2001, 05:59 PM
I'm trying to connect to a Oracle database (ver. 8.1.5) from C# (beta 2). See the code below. My problem is that I some times do connect and some times I do not. In the latter case I get the following exception:

Unhandled Exception: System.Data.OleDb.OleDbException: Oracle error occurred, but
error message could not be retrieved from Oracle.
at System.Data.OleDb.OleDbConnection.ProcessResults(Int32 hr)
at System.Data.OleDb.OleDbConnection.InitializeProvider()
at System.Data.OleDb.OleDbConnection.Open()
at ADOTest.Main(String[] args)


I can't figure out why the exception occurs occasionaly but can it be my connection string that is not optimal?

Any help is appreciated.

Regards


using System;
using System.Data.OleDb;

class ADOTest
{
public static void Main(string[] args)
{
const string connStr = "Provider=MSDAORA;Database=pultz;User ID=scott;Password=tiger";
OleDbConnection myConn = new OleDbConnection(connStr);
string myQuery = "SELECT * FROM EMP";
OleDbCommand myOleDbCommand = new OleDbCommand(myQuery);
myOleDbCommand.Connection = myConn;
myConn.Open();
Console.WriteLine("Server version: " + myConn.ServerVersion
+ "\nState: " + myConn.State.ToString());
OleDbDataReader rset = myOleDbCommand.ExecuteReader();
int i = 0;
while (rset.Read())
++i;
Console.WriteLine(i + " rows selected");
myConn.Close();
}
}

ShazBot!
November 8th, 2001, 10:58 AM
If you are attempting to connect to an ORACLE with the ADO.NET it will not work for the connection - so you have to CHEAT :)
here is an example:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Configuration;
public DataSet getList( string strUserName, string strPassword)
{
try
{
ADODB.Connection cn = new ADODB.Connection();
ADODB.Recordset rs = new ADODB.Recordset();

OleDbDataAdapter myadapter = new OleDbDataAdapter();
DataSet dsData = new DataSet("List");

string strConn = ConfigurationSettings.AppSettings["OracleConn"]; //add a key in the section of web.Config or App.Config
strConn += ";UID=" + strUserName + ";PWD="+ strPassword;

string strQuery = "select * from Listing";

cn.CursorLocation = ADODB.CursorLocationEnum.adUseClient;
cn.Open(strConn,strUserName,strPassword,-1);
rs.Open(strQuery,cn,ADODB.CursorTypeEnum.adOpenStatic,ADODB.LockTypeEnum.adLockReadOnly, (int)ADODB.CommandTypeEnum.adCmdText);

myadapter.Fill(dsData,rs,"Listing");
cn.Close();

return dsData;
}
catch(Exception e)
{
throw e;
}
}