Best way to get single value from database

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom

    #1

    Best way to get single value from database

    Here is what I do to get a single value from my database (using Oracle ODP
    as example):

    Dim ID as Object
    Dim cmdTest as New OracleCommand(" select ID from MyTable where key = " &
    KeySearch")
    ID=cmdText.Exec uteNonQuery
    If ID is Nothing then
    'Not found - do not found processing here
    Else
    'Found the ID, so do that processing here
    If Not ID is System.DBNull.V alue then
    Debug.WriteLine CLng(ID)
    End If
    End If

    I know this works fine, but my concern is having to use an Object variable
    to store the value. I know that objects cause extra processing and more
    overhead; yet, I have to be able to handle the condition of (1) It not
    finding the record, or (2) The column it returns (in this case, ID) ends up
    being NULL. If I DIMed ID as being either a string or a number, then it
    might fail and cause an exception. Sure I could trap that exception; or use
    a DataReader, but that would seem to be even more wasteful since all I want
    back is one value.

    Again, I know the above code works, but am just wondering if there is a
    better way to do it rather than using an Object variable.

    Tom


  • Trev Hunter

    #2
    Re: Best way to get single value from database

    There shouldn't be any overhead in decalring it as "Object" - after all,
    most things in .net derive from object (reference types anyway).

    I would point you in the direction of the "ExecuteSca lar" method of the
    command object though - this returns the first column of the first row in
    the results. You could change your SQL statement to something like "SELECT
    TOP 1 ID FROM MyTable WHERE Key=bla" (not sure if the oracle syntax is
    correct, but you get the idea).

    HTH,

    Trev.


    "Tom" <tom@nospam.com > wrote in message
    news:%239%23wZa N7DHA.2952@TK2M SFTNGP09.phx.gb l...[color=blue]
    > Here is what I do to get a single value from my database (using Oracle ODP
    > as example):
    >
    > Dim ID as Object
    > Dim cmdTest as New OracleCommand(" select ID from MyTable where key = " &
    > KeySearch")
    > ID=cmdText.Exec uteNonQuery
    > If ID is Nothing then
    > 'Not found - do not found processing here
    > Else
    > 'Found the ID, so do that processing here
    > If Not ID is System.DBNull.V alue then
    > Debug.WriteLine CLng(ID)
    > End If
    > End If
    >
    > I know this works fine, but my concern is having to use an Object variable
    > to store the value. I know that objects cause extra processing and more
    > overhead; yet, I have to be able to handle the condition of (1) It not
    > finding the record, or (2) The column it returns (in this case, ID) ends[/color]
    up[color=blue]
    > being NULL. If I DIMed ID as being either a string or a number, then it
    > might fail and cause an exception. Sure I could trap that exception; or[/color]
    use[color=blue]
    > a DataReader, but that would seem to be even more wasteful since all I[/color]
    want[color=blue]
    > back is one value.
    >
    > Again, I know the above code works, but am just wondering if there is a
    > better way to do it rather than using an Object variable.
    >
    > Tom
    >
    >[/color]


    Comment

    • Tom

      #3
      Re: Best way to get single value from database

      Trev: Oops, that was a typo... I meant

      ID=cmdText.Exec uteScalar

      When I was copying/translating from my code I goofed it up.

      Anyway, this seems to be an acceptable way of doing it? Is there anything
      more efficient, or does this work pretty well?

      Tom

      "Trev Hunter" <hunter_trev@ho tmail.com> wrote in message
      news:%23s4vkFO7 DHA.2064@TK2MSF TNGP11.phx.gbl. ..[color=blue]
      > There shouldn't be any overhead in decalring it as "Object" - after all,
      > most things in .net derive from object (reference types anyway).
      >
      > I would point you in the direction of the "ExecuteSca lar" method of the
      > command object though - this returns the first column of the first row in
      > the results. You could change your SQL statement to something like "SELECT
      > TOP 1 ID FROM MyTable WHERE Key=bla" (not sure if the oracle syntax is
      > correct, but you get the idea).
      >
      > HTH,
      >
      > Trev.
      >
      >
      > "Tom" <tom@nospam.com > wrote in message
      > news:%239%23wZa N7DHA.2952@TK2M SFTNGP09.phx.gb l...[color=green]
      > > Here is what I do to get a single value from my database (using Oracle[/color][/color]
      ODP[color=blue][color=green]
      > > as example):
      > >
      > > Dim ID as Object
      > > Dim cmdTest as New OracleCommand(" select ID from MyTable where key = " &
      > > KeySearch")
      > > ID=cmdText.Exec uteNonQuery
      > > If ID is Nothing then
      > > 'Not found - do not found processing here
      > > Else
      > > 'Found the ID, so do that processing here
      > > If Not ID is System.DBNull.V alue then
      > > Debug.WriteLine CLng(ID)
      > > End If
      > > End If
      > >
      > > I know this works fine, but my concern is having to use an Object[/color][/color]
      variable[color=blue][color=green]
      > > to store the value. I know that objects cause extra processing and more
      > > overhead; yet, I have to be able to handle the condition of (1) It not
      > > finding the record, or (2) The column it returns (in this case, ID) ends[/color]
      > up[color=green]
      > > being NULL. If I DIMed ID as being either a string or a number, then it
      > > might fail and cause an exception. Sure I could trap that exception; or[/color]
      > use[color=green]
      > > a DataReader, but that would seem to be even more wasteful since all I[/color]
      > want[color=green]
      > > back is one value.
      > >
      > > Again, I know the above code works, but am just wondering if there is a
      > > better way to do it rather than using an Object variable.
      > >
      > > Tom
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Trev Hunter

        #4
        Re: Best way to get single value from database

        Tom, the part about using an object is fine as you don't have much choice -
        ExecuteScalar returns an object, but I would redesign your IF statement
        slightly to make it so there is only one place where you have to do
        processing for no value being returned. Also, if you do a lot of processing
        once you have the ID, convert it to an integer once you have determined it
        is valid as in the example below.

        e.g.

        -------------------
        Dim objTemp as Object
        Dim ID as Long

        Dim cmdTest as New OracleCommand(" select ID from MyTable where key = " &
        KeySearch")

        objTemp = cmdText.Execute Scalar

        If objTemp is Nothing orelse System.DBNull.E quals(objTemp) then

        'Not found - do not found processing here

        Else

        'Found the ID, so do that processing here

        ' Convert to required datatype and use this from now on.
        ID = CLng(objTemp)

        Debug.WriteLine (ID)

        End If

        ----------------

        Just one other thing I noticed from your example: Are you sure that you want
        to use a Long? Remember that Longs in .net are 64 bits, and integers are 32
        bits. Integers are faster, but if your ID field is 64 bits, I guess you have
        to use a long.

        The rest of performance considerations are really down to what you're trying
        to do. If you're just after the one value and doing processing with it, this
        way is fine, but if you're doing this repeatedly, grab a group of ID's at
        one time to cut down on calls to the database.

        HTH,

        Trev.


        "Tom" <tom@nospam.com > wrote in message
        news:O62G5SO7DH A.452@TK2MSFTNG P11.phx.gbl...[color=blue]
        > Trev: Oops, that was a typo... I meant
        >
        > ID=cmdText.Exec uteScalar
        >
        > When I was copying/translating from my code I goofed it up.
        >
        > Anyway, this seems to be an acceptable way of doing it? Is there anything
        > more efficient, or does this work pretty well?
        >
        > Tom[/color]
        [color=blue]
        >[/color]


        Comment

        • Tom

          #5
          Re: Best way to get single value from database

          Trev: Thanks for the info. I will take into account all your advice.

          One final question: You use System.DBNull.E quals(objTemp). .. would the
          following be equivalent?

          objTemp is System.DBNull.V alue

          The way nulls are handled in VB.NET is quite different than in VB 6. Thanks
          again.

          Tom

          "Trev Hunter" <hunter_trev@ho tmail.com> wrote in message
          news:eu6bvNX7DH A.1632@TK2MSFTN GP12.phx.gbl...[color=blue]
          > Tom, the part about using an object is fine as you don't have much[/color]
          choice -[color=blue]
          > ExecuteScalar returns an object, but I would redesign your IF statement
          > slightly to make it so there is only one place where you have to do
          > processing for no value being returned. Also, if you do a lot of[/color]
          processing[color=blue]
          > once you have the ID, convert it to an integer once you have determined it
          > is valid as in the example below.
          >
          > e.g.
          >
          > -------------------
          > Dim objTemp as Object
          > Dim ID as Long
          >
          > Dim cmdTest as New OracleCommand(" select ID from MyTable where key = " &
          > KeySearch")
          >
          > objTemp = cmdText.Execute Scalar
          >
          > If objTemp is Nothing orelse System.DBNull.E quals(objTemp) then
          >
          > 'Not found - do not found processing here
          >
          > Else
          >
          > 'Found the ID, so do that processing here
          >
          > ' Convert to required datatype and use this from now on.
          > ID = CLng(objTemp)
          >
          > Debug.WriteLine (ID)
          >
          > End If
          >
          > ----------------
          >
          > Just one other thing I noticed from your example: Are you sure that you[/color]
          want[color=blue]
          > to use a Long? Remember that Longs in .net are 64 bits, and integers are[/color]
          32[color=blue]
          > bits. Integers are faster, but if your ID field is 64 bits, I guess you[/color]
          have[color=blue]
          > to use a long.
          >
          > The rest of performance considerations are really down to what you're[/color]
          trying[color=blue]
          > to do. If you're just after the one value and doing processing with it,[/color]
          this[color=blue]
          > way is fine, but if you're doing this repeatedly, grab a group of ID's at
          > one time to cut down on calls to the database.
          >
          > HTH,
          >
          > Trev.
          >
          >
          > "Tom" <tom@nospam.com > wrote in message
          > news:O62G5SO7DH A.452@TK2MSFTNG P11.phx.gbl...[color=green]
          > > Trev: Oops, that was a typo... I meant
          > >
          > > ID=cmdText.Exec uteScalar
          > >
          > > When I was copying/translating from my code I goofed it up.
          > >
          > > Anyway, this seems to be an acceptable way of doing it? Is there[/color][/color]
          anything[color=blue][color=green]
          > > more efficient, or does this work pretty well?
          > >
          > > Tom[/color]
          >[color=green]
          > >[/color]
          >
          >[/color]


          Comment

          • Trev Hunter

            #6
            Re: Best way to get single value from database

            > One final question: You use System.DBNull.E quals(objTemp). .. would the[color=blue]
            > following be equivalent?
            >
            > objTemp is System.DBNull.V alue[/color]

            With DBNull, both ways are the same because DBNull is a singleton (i.e.
            there is only one instance of it). The reason why I tend to use equals is
            because the "is" operator checks *reference equality* (i.e. two variables
            point to the same object). However, the "Equals" method can be overridden by
            the designer of a class so it retuns true for *value equality* (two
            different object instances that represent the same thing).


            Hth,

            Trev.


            "Tom" <tom@nospam.com > wrote in message
            news:uwcTEiy7DH A.1672@TK2MSFTN GP12.phx.gbl...[color=blue]
            > Trev: Thanks for the info. I will take into account all your advice.
            >
            > One final question: You use System.DBNull.E quals(objTemp). .. would the
            > following be equivalent?
            >
            > objTemp is System.DBNull.V alue
            >
            > The way nulls are handled in VB.NET is quite different than in VB 6.[/color]
            Thanks[color=blue]
            > again.
            >
            > Tom
            >
            > "Trev Hunter" <hunter_trev@ho tmail.com> wrote in message
            > news:eu6bvNX7DH A.1632@TK2MSFTN GP12.phx.gbl...[color=green]
            > > Tom, the part about using an object is fine as you don't have much[/color]
            > choice -[color=green]
            > > ExecuteScalar returns an object, but I would redesign your IF statement
            > > slightly to make it so there is only one place where you have to do
            > > processing for no value being returned. Also, if you do a lot of[/color]
            > processing[color=green]
            > > once you have the ID, convert it to an integer once you have determined[/color][/color]
            it[color=blue][color=green]
            > > is valid as in the example below.
            > >
            > > e.g.
            > >
            > > -------------------
            > > Dim objTemp as Object
            > > Dim ID as Long
            > >
            > > Dim cmdTest as New OracleCommand(" select ID from MyTable where key = " &
            > > KeySearch")
            > >
            > > objTemp = cmdText.Execute Scalar
            > >
            > > If objTemp is Nothing orelse System.DBNull.E quals(objTemp) then
            > >
            > > 'Not found - do not found processing here
            > >
            > > Else
            > >
            > > 'Found the ID, so do that processing here
            > >
            > > ' Convert to required datatype and use this from now on.
            > > ID = CLng(objTemp)
            > >
            > > Debug.WriteLine (ID)
            > >
            > > End If
            > >
            > > ----------------
            > >
            > > Just one other thing I noticed from your example: Are you sure that you[/color]
            > want[color=green]
            > > to use a Long? Remember that Longs in .net are 64 bits, and integers are[/color]
            > 32[color=green]
            > > bits. Integers are faster, but if your ID field is 64 bits, I guess you[/color]
            > have[color=green]
            > > to use a long.
            > >
            > > The rest of performance considerations are really down to what you're[/color]
            > trying[color=green]
            > > to do. If you're just after the one value and doing processing with it,[/color]
            > this[color=green]
            > > way is fine, but if you're doing this repeatedly, grab a group of ID's[/color][/color]
            at[color=blue][color=green]
            > > one time to cut down on calls to the database.
            > >
            > > HTH,
            > >
            > > Trev.
            > >
            > >
            > > "Tom" <tom@nospam.com > wrote in message
            > > news:O62G5SO7DH A.452@TK2MSFTNG P11.phx.gbl...[color=darkred]
            > > > Trev: Oops, that was a typo... I meant
            > > >
            > > > ID=cmdText.Exec uteScalar
            > > >
            > > > When I was copying/translating from my code I goofed it up.
            > > >
            > > > Anyway, this seems to be an acceptable way of doing it? Is there[/color][/color]
            > anything[color=green][color=darkred]
            > > > more efficient, or does this work pretty well?
            > > >
            > > > Tom[/color]
            > >[color=darkred]
            > > >[/color]
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • smay

              #7
              Re: Best way to get single value from database

              Do you know how to use ExecuteScalar to get a single value from an Oracle stored procedure. It only returns 'null' when the out parameter is a REF CURSOR, and it doesn't appear to work for an out param of Number either

              ----- Trev Hunter wrote: ----

              There shouldn't be any overhead in decalring it as "Object" - after all
              most things in .net derive from object (reference types anyway)

              I would point you in the direction of the "ExecuteSca lar" method of th
              command object though - this returns the first column of the first row i
              the results. You could change your SQL statement to something like "SELEC
              TOP 1 ID FROM MyTable WHERE Key=bla" (not sure if the oracle syntax i
              correct, but you get the idea)

              HTH

              Trev


              "Tom" <tom@nospam.com > wrote in messag
              news:%239%23wZa N7DHA.2952@TK2M SFTNGP09.phx.gb l..[color=blue]
              > Here is what I do to get a single value from my database (using Oracle OD
              > as example)[color=green]
              >> Dim ID as Objec[/color]
              > Dim cmdTest as New OracleCommand(" select ID from MyTable where key = " &> KeySearch"
              > ID=cmdText.Exec uteNonQuer
              > If ID is Nothing the
              > 'Not found - do not found processing her
              > Els
              > 'Found the ID, so do that processing her
              > If Not ID is System.DBNull.V alue the
              > Debug.WriteLine CLng(ID
              > End I
              > End I[color=green]
              >> I know this works fine, but my concern is having to use an Object variabl[/color]
              > to store the value. I know that objects cause extra processing and mor
              > overhead; yet, I have to be able to handle the condition of (1) It no
              > finding the record, or (2) The column it returns (in this case, ID) end[/color]
              u[color=blue]
              > being NULL. If I DIMed ID as being either a string or a number, then i
              > might fail and cause an exception. Sure I could trap that exception; o[/color]
              us[color=blue]
              > a DataReader, but that would seem to be even more wasteful since all[/color]
              wan[color=blue]
              > back is one value[color=green]
              >> Again, I know the above code works, but am just wondering if there is[/color]
              > better way to do it rather than using an Object variable[color=green]
              >> To
              >>[/color][/color]

              Comment

              • smay

                #8
                Re: Best way to get single value from database

                Do you know how to use ExecuteScalar to get a single value from an Oracle stored procedure. It only returns 'null' when the out parameter is a REF CURSOR, and it doesn't appear to work for an out param of Number either

                ----- Trev Hunter wrote: ----

                There shouldn't be any overhead in decalring it as "Object" - after all
                most things in .net derive from object (reference types anyway)

                I would point you in the direction of the "ExecuteSca lar" method of th
                command object though - this returns the first column of the first row i
                the results. You could change your SQL statement to something like "SELEC
                TOP 1 ID FROM MyTable WHERE Key=bla" (not sure if the oracle syntax i
                correct, but you get the idea)

                HTH

                Trev


                "Tom" <tom@nospam.com > wrote in messag
                news:%239%23wZa N7DHA.2952@TK2M SFTNGP09.phx.gb l..[color=blue]
                > Here is what I do to get a single value from my database (using Oracle OD
                > as example)[color=green]
                >> Dim ID as Objec[/color]
                > Dim cmdTest as New OracleCommand(" select ID from MyTable where key = " &> KeySearch"
                > ID=cmdText.Exec uteNonQuer
                > If ID is Nothing the
                > 'Not found - do not found processing her
                > Els
                > 'Found the ID, so do that processing her
                > If Not ID is System.DBNull.V alue the
                > Debug.WriteLine CLng(ID
                > End I
                > End I[color=green]
                >> I know this works fine, but my concern is having to use an Object variabl[/color]
                > to store the value. I know that objects cause extra processing and mor
                > overhead; yet, I have to be able to handle the condition of (1) It no
                > finding the record, or (2) The column it returns (in this case, ID) end[/color]
                u[color=blue]
                > being NULL. If I DIMed ID as being either a string or a number, then i
                > might fail and cause an exception. Sure I could trap that exception; o[/color]
                us[color=blue]
                > a DataReader, but that would seem to be even more wasteful since all[/color]
                wan[color=blue]
                > back is one value[color=green]
                >> Again, I know the above code works, but am just wondering if there is[/color]
                > better way to do it rather than using an Object variable[color=green]
                >> To
                >>[/color][/color]

                Comment

                • smay

                  #9
                  Re: Best way to get single value from database

                  Do you know how to use ExecuteScalar to get a single value from an Oracle stored procedure. It only returns 'null' when the out parameter is a REF CURSOR, and it doesn't appear to work for an out param of Number either

                  ----- Trev Hunter wrote: ----

                  There shouldn't be any overhead in decalring it as "Object" - after all
                  most things in .net derive from object (reference types anyway)

                  I would point you in the direction of the "ExecuteSca lar" method of th
                  command object though - this returns the first column of the first row i
                  the results. You could change your SQL statement to something like "SELEC
                  TOP 1 ID FROM MyTable WHERE Key=bla" (not sure if the oracle syntax i
                  correct, but you get the idea)

                  HTH

                  Trev


                  "Tom" <tom@nospam.com > wrote in messag
                  news:%239%23wZa N7DHA.2952@TK2M SFTNGP09.phx.gb l..[color=blue]
                  > Here is what I do to get a single value from my database (using Oracle OD
                  > as example)[color=green]
                  >> Dim ID as Objec[/color]
                  > Dim cmdTest as New OracleCommand(" select ID from MyTable where key = " &> KeySearch"
                  > ID=cmdText.Exec uteNonQuer
                  > If ID is Nothing the
                  > 'Not found - do not found processing her
                  > Els
                  > 'Found the ID, so do that processing her
                  > If Not ID is System.DBNull.V alue the
                  > Debug.WriteLine CLng(ID
                  > End I
                  > End I[color=green]
                  >> I know this works fine, but my concern is having to use an Object variabl[/color]
                  > to store the value. I know that objects cause extra processing and mor
                  > overhead; yet, I have to be able to handle the condition of (1) It no
                  > finding the record, or (2) The column it returns (in this case, ID) end[/color]
                  u[color=blue]
                  > being NULL. If I DIMed ID as being either a string or a number, then i
                  > might fail and cause an exception. Sure I could trap that exception; o[/color]
                  us[color=blue]
                  > a DataReader, but that would seem to be even more wasteful since all[/color]
                  wan[color=blue]
                  > back is one value[color=green]
                  >> Again, I know the above code works, but am just wondering if there is[/color]
                  > better way to do it rather than using an Object variable[color=green]
                  >> To
                  >>[/color][/color]

                  Comment

                  Working...