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
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Get a datarow with a query OLEDB - VB.net

Get a datarow with a query OLEDB - VB.net

Scheduled Pinned Locked Moved Database
databasecsharp
5 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.
  • R Offline
    R Offline
    Richard Berry100
    wrote on last edited by
    #1

    Is there a better way to get one row from a database. I have been using the Fill Method of the DataAdapter to fill a datatable, and then returning the first row of the table

    Dim dr As DataRow
    Using cn As New OleDb.OleDbConnection(cnStr)
    Dim cmd As New OleDb.OleDbCommand(strSelect, cn)
    Dim da As New OleDb.OleDbDataAdapter(cmd)

            Try
                cn.Open()
                da.Fill(dt)
                If dt.Rows.Count >= 1 Then
                    dr = dt.Rows(0)
                End If
            Catch ex As Exception
                'Err handling
            Finally
                cn.Close()
                dt = Nothing
            End Try
        End Using
        Return dr
    
    P 1 Reply Last reply
    0
    • R Richard Berry100

      Is there a better way to get one row from a database. I have been using the Fill Method of the DataAdapter to fill a datatable, and then returning the first row of the table

      Dim dr As DataRow
      Using cn As New OleDb.OleDbConnection(cnStr)
      Dim cmd As New OleDb.OleDbCommand(strSelect, cn)
      Dim da As New OleDb.OleDbDataAdapter(cmd)

              Try
                  cn.Open()
                  da.Fill(dt)
                  If dt.Rows.Count >= 1 Then
                      dr = dt.Rows(0)
                  End If
              Catch ex As Exception
                  'Err handling
              Finally
                  cn.Close()
                  dt = Nothing
              End Try
          End Using
          Return dr
      
      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      There sure is. For one thing, if you're using SQL Server you can use SELECT TOP 1 ... If your database doesn't support that, you can use a DataReader and read only one row (but it won't be a DataRow).

      R 1 Reply Last reply
      0
      • P PIEBALDconsult

        There sure is. For one thing, if you're using SQL Server you can use SELECT TOP 1 ... If your database doesn't support that, you can use a DataReader and read only one row (but it won't be a DataRow).

        R Offline
        R Offline
        Richard Berry100
        wrote on last edited by
        #3

        Thanks PIEBALDconsult The SELECT query should return only one row based on conditions in the WHERE clause, but I was wondering if there was a way to eliminate declaring a DataTable, and then equating the DataRow to the first row from the DataTable?

        P L 2 Replies Last reply
        0
        • R Richard Berry100

          Thanks PIEBALDconsult The SELECT query should return only one row based on conditions in the WHERE clause, but I was wondering if there was a way to eliminate declaring a DataTable, and then equating the DataRow to the first row from the DataTable?

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Richard.Berry100 wrote:

          only one row based on conditions in the WHERE clause

          Oh, that's alright then. You probably needn't be too concerned in that case -- if it solves the problem so you can move on and then assess performance and such at a later date. Other than that, I use a DataReader; I avoid DataAdapters except when I'm very lazy -- and then it always bites me back.

          1 Reply Last reply
          0
          • R Richard Berry100

            Thanks PIEBALDconsult The SELECT query should return only one row based on conditions in the WHERE clause, but I was wondering if there was a way to eliminate declaring a DataTable, and then equating the DataRow to the first row from the DataTable?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Richard.Berry100 wrote:

            but I was wondering if there was a way to eliminate declaring a DataTable, and then equating the DataRow to the first row from the DataTable?

            Why? You can simply load the reader in your datatable and return the first row from a method. That way the table is out of scope and discarded. If you really think that's consuming too much memory, then you should not use a DataRow, but an array of values[^]. Take a look at the section[^] that says "Working with DataReaders".

            Bastard Programmer from Hell :suss:

            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