user1501034
user1501034

Reputation: 363

Fetching data from oracle DB

I use the below code to connect to my oracle DB and execute a query. The query I used in the example simply fetches a set of rows from a table. However, I keep getting an error message that "The table or view does not exist". But, I am pretty sure that the table exists in the DB. Where am I going wrong?

 public void UpdateDatabase()
        {
            System.Data.OracleClient.OracleConnection conn = new System.Data.OracleClient.OracleConnection();
            conn.ConnectionString = "Data Source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.5.144)(PORT = 1521))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = orcl)));UID=mwm;PWD=mwm";
            conn.Open();
            OracleCommand command = conn.CreateCommand();
            command.CommandText = "Select * from Task"; 
            command.ExecuteNonQuery();
            command.Dispose();
        }

The error is triggered when command.ExecuteNonQuery() is reached.

Upvotes: 1

Views: 20251

Answers (2)

Asif Mushtaq
Asif Mushtaq

Reputation: 13150

I think the problem is with command.ExecuteNonQuery();

Actually you are executing a query here therefore you should use either DataAdapter or DataReader.

public void UpdateDatabase()
{
   System.Data.OracleClient.OracleConnection conn = new    System.Data.OracleClient.OracleConnection();
  conn.ConnectionString = "Data Source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.5.144)(PORT = 1521))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = orcl)));UID=mwm;PWD=mwm";
     conn.Open();
     OracleCommand command = conn.CreateCommand();            
     SqlDataAdapter a = new SqlDataAdapter("Select * from \"Task\"", command))            
     DataTable t = new DataTable();
     a.Fill(t);
     command.Dispose();
}

Upvotes: 1

Habib
Habib

Reputation: 223227

Task is oracle Reserve Word, that is why you are getting this error. Use double quotes.

command.CommandText = "Select * from \"Task\"";

ExectueNonQuery, may not give you any error, but it will not give you the desired result. You need to do command.ExecuteReader. See the link.

You may also see this Getting Started with Oracle Data Provider for .NET (C# Version)

Upvotes: 1

Related Questions