0

I am trying to connect to an oracle database using C#.

Here is my code:

      OracleConnection conn = new OracleConnection(); // C#
           conn.ConnectionString = oradb;
        conn.Open();
        string sql = " select department_name from departments where department_id = 10"; // C#
        OracleCommand cmd = new OracleCommand();
        cmd.CommandText = sql;
        cmd.Connection = conn;
        cmd.CommandType = CommandType.Text; ///this is the line that gives the error

What is the proper way to set the command type? Thank you.

5
  • 1
    How about executing command without specify CommandType? By the way can you reveal more code here? Commented Aug 25, 2014 at 14:38
  • Hassan Nisar do you mean where is the cmd.ExecuteDataReader command..? Commented Aug 25, 2014 at 14:41
  • Just remove that line. Commented Aug 25, 2014 at 14:42
  • here is a good SO example to follow I suggest you get familiar with using statement and how to wrap your code for disposing automatically as well stackoverflow.com/questions/3101786/… also learn how to use Parameterized querys I doubt that you will be wanting to hard code your Where Filter Commented Aug 25, 2014 at 14:42
  • @DJKRAZE. I expect OP has not posted cmd.ExecuteReader() intentionally. Commented Aug 25, 2014 at 14:43

2 Answers 2

2

Using Store Procedure:

using (OracleConnection conn = new OracleConnection( oradb ))
{
      conn.Open();
      OracleCommand cmd = new OracleCommand("StoreProcedureName", con);
      cmd.CommandType = CommandType.StoredProcedure;   

      //specify command parameters 
      //and Direction

      using(OracleDataReader reader = cmd.ExecuteReader())
      {
          while (reader.Read())
          {
             //string s = reader.GetInt32(0) + ", " + reader.GetInt32(1);
          }
      }                   
}

CommandType.Text: (not mandatory to specify CommandType).

using (OracleConnection conn = new OracleConnection( oradb ))
{
      string sql = @"SELECT department_name FROM departments 
                     WHERE department_id = @department_id";
      conn.Open();
      OracleCommand cmd = new OracleCommand(sql, conn);

      //specify command parameters
      cmd.Parameters.Add(new OracleParameter("@department_id", 10));

      using(OracleDataReader reader = cmd.ExecuteReader())
      {
          while (reader.Read())
          {
             //string s = reader.GetString(0);
          }
      }                   
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes @Brett. I intentionally left it out to improve readability.
1

Make sure you toss each of these pieces in a using() statement, i.e.

using( OracleConnection conn = new OracleConnection( oradb ) )
{
    conn.Open();

    using( OracleCommand cmd = new OracleCommand( "sql here", conn ) )
    {
        //cmd.Execute(); cmd.ExecuteNonQuery();
    }
}

4 Comments

how do I set the cmd to be Text vs Stored procedure?
This is a good answer -- the usings are critical here as you will certainly cause a memory leak otherwise (can testify from experience). The default command type is Text, so you should be all set. See: docs.oracle.com/cd/B19306_01/win.102/b14307/… I only recall changing the CommandType when I wanted to execute a stored procedure.
@Brett when you want to do a stored procedure do you do it like Hassan's answer or some other way?
Hassan's answer is more complete, and, yes, that's what I would do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.