Jason94
Jason94

Reputation: 13610

Problems reading from Oracle

I've just installed an Oracle express database and am trying to read some data from a table I've put in there:

using (OracleConnection conn = new OracleConnection("Data Source=localhost:1521/xe;Persist Security Info=True;User ID=SYSTEM;Password=SYSTEMPASSWORD"))
{
    OracleCommand command = new OracleCommand("SELECT * FROM Persons WHERE Firstname = 'John'", conn);
    conn.Open();
    OracleDataReader reader = command.ExecuteReader();

    try
    {
        while (reader.Read())
        {
            string strResult = reader.GetString(0);
        }
    }
    catch (OracleException oex)
    {
        MessageBox.Show(oex.Message, "Oracle error");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error");
    }
    finally
    {
        reader.Close();
    }
} 

On the while (reader.Read()) it just quits since the reader does not hold any data. What is wrong? Connectionstring? I've run the same SELECT in the commandprompt tool that is installed with Oracle express and it works fine.

Upvotes: 6

Views: 2014

Answers (4)

mattyB
mattyB

Reputation: 1104

Bit late to the party but...

I had the same problem accessing a view and it turned out the appropriate privileges weren't granted on it. Once I'd granted the SELECT privilege to my user id I could get my data.

Upvotes: 1

CarlosJ
CarlosJ

Reputation: 244

It´s been a long time since I don't use Oracle with .Net, but I don´t see which database you use (is it "xe"?). With SQL Server I specify it with INITIAL CATALOG in the connection string. Here you have different connection strings to try: http://www.connectionstrings.com/oracle. I would also start by openning the connection.

Upvotes: 1

Florin Ghita
Florin Ghita

Reputation: 17643

Table Persons is in the SYSTEM schema? Run another query, like 'select object_name from user_objects' and give us updates with what you've done.

Upvotes: 2

user123664
user123664

Reputation:

First thing to do when connection to any system is see/test if it succeeded and after that continue. Without these simple kinds of tests your application is bound to behave like a time-bomb. A bit of defensive programming will make your projects a lot easier to debug. Not really the answer you are looking for but currently the state of the connection is not clear at query execution time.

Upvotes: 5

Related Questions