Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Retrieving a DateTime value with OracleClient

Retrieving a DateTime value with OracleClient

Scheduled Pinned Locked Moved Database
csharpquestiondatabaseoraclesysadmin
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bernhard Hiller
    wrote on last edited by
    #1

    Hello, I am trying to read a DateTime value from an Oracle database (Oracle Version 11.2.0.1.0 running on a German Windows XP). The column is defined as "DATE", and the value is correctly shown as "2010-05-04 16:17:10.0" via the web browser when viewing all the data of the respective table. But when I retrieve the data with the OracleClient (via OracleCommand and OracleDataReader) in a C# application (.NET 2), I receive a string (!) value of

    "04.05.10 "

    , i.e. reader["LASTLOGIN"] returns a string with that value which I then convert to a DateTime using the Convert.ToDateTime() function. By the way, a

                int lastLoginCol = reader.GetOrdinal("LASTLOGIN");
                LastLogin = reader.GetDateTime(lastLoginCol);
    

    yields a "Specific cast is not valid" error message. Please note the 6 blanks after the date part. Where has the time part gone to? How can I retrieve the full date time value? Is the answer: "Not at all, it's Oracle"? And what about a situation where the server is running with a different locale than the client? Is it really necessary to change to CHAR 14 (i.e. YYYYMMDDhhmmss - that does work with some tricks)? Thanks a lot for your ideas! Bernhard

    C 1 Reply Last reply
    0
    • B Bernhard Hiller

      Hello, I am trying to read a DateTime value from an Oracle database (Oracle Version 11.2.0.1.0 running on a German Windows XP). The column is defined as "DATE", and the value is correctly shown as "2010-05-04 16:17:10.0" via the web browser when viewing all the data of the respective table. But when I retrieve the data with the OracleClient (via OracleCommand and OracleDataReader) in a C# application (.NET 2), I receive a string (!) value of

      "04.05.10 "

      , i.e. reader["LASTLOGIN"] returns a string with that value which I then convert to a DateTime using the Convert.ToDateTime() function. By the way, a

                  int lastLoginCol = reader.GetOrdinal("LASTLOGIN");
                  LastLogin = reader.GetDateTime(lastLoginCol);
      

      yields a "Specific cast is not valid" error message. Please note the 6 blanks after the date part. Where has the time part gone to? How can I retrieve the full date time value? Is the answer: "Not at all, it's Oracle"? And what about a situation where the server is running with a different locale than the client? Is it really necessary to change to CHAR 14 (i.e. YYYYMMDDhhmmss - that does work with some tricks)? Thanks a lot for your ideas! Bernhard

      C Offline
      C Offline
      Chris Meech
      wrote on last edited by
      #2

      The Oracle DATE type for the column does not have a corresponding equivalent within most languages you will use. As a result it must be cast to some other intrinsic type. Usually what will happen is that Oracle will convert it to a string using a session specific date format specification. Look up on NLS_DATE_FORMAT and if you don't know what that format is, the following might help you

      SELECT value FROM v$nls_parameters WHERE parameter ='NLS_DATE_FORMAT';

      :)

      Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

      D 1 Reply Last reply
      0
      • C Chris Meech

        The Oracle DATE type for the column does not have a corresponding equivalent within most languages you will use. As a result it must be cast to some other intrinsic type. Usually what will happen is that Oracle will convert it to a string using a session specific date format specification. Look up on NLS_DATE_FORMAT and if you don't know what that format is, the following might help you

        SELECT value FROM v$nls_parameters WHERE parameter ='NLS_DATE_FORMAT';

        :)

        Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

        D Offline
        D Offline
        David Skelly
        wrote on last edited by
        #3

        In .NET all you have to do is open an OracleDataReader from an OracleCommand and then call GetDateTime. It works for me. .NET should handle all the type conversion issues: the OracleDataReader "knows" how to convert SQL types to .NET types so that I don't have to. It's called encapsulation. Or maybe abstraction, I can never remember the difference between those two.

        B 1 Reply Last reply
        0
        • D David Skelly

          In .NET all you have to do is open an OracleDataReader from an OracleCommand and then call GetDateTime. It works for me. .NET should handle all the type conversion issues: the OracleDataReader "knows" how to convert SQL types to .NET types so that I don't have to. It's called encapsulation. Or maybe abstraction, I can never remember the difference between those two.

          B Offline
          B Offline
          Bernhard Hiller
          wrote on last edited by
          #4

          Thanks a lot for your input. The problem has dissolved somehow - I use to shutdown the server and the client in the evening and start them again next morning, and now the problem cannot be reproduced. Maybe there was some inconsistent state in the Oracle database, during development things can go wrong and Oracle likes to punish you horribly for the smallest misdeeds....

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups