Parameters in OracleCommand CommandText ?

Last post 06-01-2007 4:57 PM by jfmccarthy. 5 replies.

Sort Posts:

  • Parameters in OracleCommand CommandText ?

    05-23-2007, 12:46 PM
    • Loading...
    • aidoco
    • Joined on 06-05-2006, 9:40 PM
    • Posts 84

    Hi
    I'm really hoping someone can help with this,

    I am trying to add parammeters to a sql statement that is inside a OracleCommand CommandText.
    I get them from the queryString initially, I know that in oracle you must put a : before the variable name
    What would the syntax of MyCommand.Paramters.Add be in the case of a sql string being used?


    It is the  values '01-MAY-2007' and '10-MAY-2007' I'm trying to paramterize, replacing them with variables from the query string
    Can I do this without having to write stored procedures?
    Thank you very very much

         

    1   // ...
    2 string startdate = "";
    3 string enddate = "";
    4 if (Request.QueryString["dtstart"] != "")
    5 {
    6 startdate = Request.QueryString["dtstart"];
    7 }
    8 if (Request.QueryString["dtend"] != "")
    9 {
    10 enddate = Request.QueryString["dtend"];
    11 }
    12
    13
    14 // ....
    15
    16 OracleConnection oracleConn = new OracleConnection();
    17 oracleConn.ConnectionString = "Data Source=Z;User ID=xxx;Password=xxx;Unicode=True";
    18 oracleConn.Open();
    19
    20 OracleCommand myCommand = oracleConn.CreateCommand();
    21 myCommand.CommandText="SELECT COUNT(*) FROM TBLThings WHERE enteredDate BETWEEN '01-MAY-2007' AND '10-MAY-07')";
    22 //myCommand.commandType = CommandType.Text;
    23 myCommand.ExecuteNonQuery();
     
    AiDo
  • Re: Parameters in OracleCommand CommandText ?

    05-23-2007, 12:52 PM
    • Loading...
    • stiletto
    • Joined on 07-10-2003, 8:42 AM
    • Louisville, KY
    • Posts 3,154

               myCommand.CommandText="SELECT COUNT(*) FROM TBLThings WHERE enteredDate  BETWEEN :StartDate AND :EndDate)";
               myCommand.Parameters.AddWithValue(":StartDate", startdate);
               myCommand.Parameters.AddWithValue(":EndDate", enddate);

  • Re: Parameters in OracleCommand CommandText ?

    05-24-2007, 9:41 AM
    • Loading...
    • aidoco
    • Joined on 06-05-2006, 9:40 PM
    • Posts 84

     thank you Star, I'll try that . looks so simple when you kno how right !

    Smile 

    AiDo
  • Re: Parameters in OracleCommand CommandText ?

    05-28-2007, 12:12 PM
    • Loading...
    • aidoco
    • Joined on 06-05-2006, 9:40 PM
    • Posts 84

     Hi Star

    Thanks for your help. it was very useful, however when I tried to modify it for an update statement it didnt work.

    Any thoughts on  why parameterizing this way  does not work for me?

    Once again , thank you 

      

    //get values from query string ...

    myCommand.CommandText = "UPDATE totals SET count1= (SELECT COUNT(*) FROM things WHERE date BETWEEN :StartDate AND :EndDate) ";
     
    myCommand.Parameters.AddWithValue(":StartDate", startdate);
    myCommand.Parameters.AddWithValue(":EndDate", enddate);
    myCommand.ExecuteNonQuery();
     
     
     
    AiDo
  • Re: Parameters in OracleCommand CommandText ?

    05-28-2007, 7:31 PM

     Your update statement will update count1 column of all the records in Totals table. Is that what you want?

    I can't see anything wrong with the code you have posted (provided you want to update all rows).

    Why do you think this is not working?

    Regards,
    Prashant


    Dont forget to click "Mark as Answer" on the post that helped you.
  • Re: Parameters in OracleCommand CommandText ?

    06-01-2007, 4:57 PM
    Answer
    • Loading...
    • jfmccarthy
    • Joined on 10-04-2006, 12:18 PM
    • Oklahoma
    • Posts 92

    When adding the parameters you should omit the : colon, it's only needed in your update statement.

    myCommand.CommandText = "UPDATE totals SET count1= (SELECT COUNT(*) FROM things WHERE date BETWEEN :StartDate AND :EndDate) ";

    myCommand.Parameters.AddWithValue("StartDate", startdate);
    myCommand.Parameters.AddWithValue("EndDate", enddate);
    myCommand.ExecuteNonQuery();

    Life would be so much easier if we only had the source code.
Page 1 of 1 (6 items)
Microsoft Communities
Page view counter