Results 1 to 13 of 13

Thread: ADO.NET pass a single record from dbReader

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    ADO.NET pass a single record from dbReader

    using the dbReader I loop through a table and need to pass each ROW to a function. Anyone know what property, method of the SqlDataReader I can use to get the full row and pass it
    Last edited by kleinma; Jul 21st, 2004 at 11:01 AM.

  2. #2
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    Why not just pass a reference to the DataReader itself?
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  3. #3
    Hyperactive Member LeeSalter's Avatar
    Join Date
    Oct 2002
    Location
    Notts, England
    Posts
    307
    If you use the OleDBDataReader you can use it like this:-

    Code:
     Dim cmd1 As New OleDbCommand
     
     cmd1.Connection = cnn1 'your database connection object
     cmd1.CommandText = _
                "SELECT * FROM TABLENAME"
    
     Dim drd1 As OleDbDataReader
     
     drd1 = cmd1.ExecuteReader
     do while drd1.read
          'each row is returned here...
          messagebox.show(drd1.GetString(ColumnNumber))
     loop
    HTH
    "I'm Brian and so is my Wife"

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Second SeanGray's answer...

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Originally posted by SeanGrebey
    Why not just pass a reference to the DataReader itself?
    Because that's inefficient. That's why.

    Originally posted by LeeSalter
    If you use the OleDBDataReader you can use it like this:-

    Code:
     Dim cmd1 As New OleDbCommand
     
     cmd1.Connection = cnn1 'your database connection object
     cmd1.CommandText = _
                "SELECT * FROM TABLENAME"
    
     Dim drd1 As OleDbDataReader
     
     drd1 = cmd1.ExecuteReader
     do while drd1.read
          'each row is returned here...
          messagebox.show(drd1.GetString(ColumnNumber))
     loop
    HTH
    I'm fairly new to .NET, but know enough that that isn't much help. Doesn't pass the entire row, just the specified column's value.

    Ok, on to the issue. Kleinma, see what you can find out about DataRow... I use it when dealing with DataSets/DataTables in an app I'm working on. I don't know if it works the same for a Reader as it does for the DT (which is what I'm currently using).

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    I don't see how passing a datareader by reference is inefficient.

    Also, DataRow does not exist for a datareader, you would have to populate a table first, then pass the appropriate row. That may work for his solution, it may be unncessary.

    If Kleinma still have issues with passing a datareader by reference, he can simply declare an empty object array and use it as the target of the DataReader.GetValues() method.

    VB Code:
    1. While dr.Read()
    2.  Dim o() As Object
    3.  dr.GetValues(o)
    4.  CallAFunction(o)
    5. End While

    If all he wants is a common area to inspect column values, passing the datareader by reference to a common function is fine. You only have to worry about closing the connection properly.

  7. #7
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Originally posted by techgnome
    Because that's inefficient. That's why.
    That isn't true at all, you are passing a reference to an object, which would be WAY more efficient than passing a row of values.

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Originally posted by hellswraith
    That isn't true at all, you are passing a reference to an object, which would be WAY more efficient than passing a row of values.
    Uncle, uncle, uncle..... OK, I'll conceed that, BUT, we don't know what the requirements are either. Maybe he's calling a func he doesn't have control over and can't use the object ref.... who knows...

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    If he has to have a DataRow, he might as well fill a datatable instead of a datareader and step through that instead. Otherwise I stand by my pass a ref to the DataReader statement.
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  10. #10
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Ok... here's the final say( to have a datarow passed, in case you dont want to pass a ref variable to the datareader, or its a
    function you can't control and it NEEDS a datarow):
    VB Code:
    1. Dim dr As System.Data.SqlClient.SqlDataReader
    2.         dr = sqlcommand1.ExecuteReader(CommandBehavior.CloseConnection)
    3.         Dim dbe As New System.Data.Common.DbEnumerator(dr)
    4.         Dim dbr As System.Data.Common.DbDataRecord
    5.         dbe.MoveNext()
    6.         While dr.Read
    7.             dbr = dbe.Current
    8.             callme(dbr)
    9.             dbe.MoveNext()
    10.         End While
    11.         dr.Close()
    12.  
    13.     End Sub
    14.  
    15.     Private Function callme(ByVal f As System.Data.Common.DbDataRecord)
    16.         MessageBox.Show(f(0))
    17.          'or
    18.          'Messagebox.show(f("COLUMNNAME"))
    19.     End Function
    Last edited by nemaroller; Jul 21st, 2004 at 01:23 PM.

  11. #11

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    sorry guys was at lunch, but i am thankful for the posts.

    The reason I didnt want to pass the entire reader was because im loading a collection object called PRODCUTS, but the load method itself is a friend function in the PRODUCT class, not the products class.. because all the variables in the PRODUCT class are private, and the public properties are read only. So I can't just create a new PRODUCT object in the PRODUCTS class, and load each PRODUCT and add it to the collection

    I also didnt want to call a new DB query for EACH product to load it as that would be stupid

    so what I wanted it to do was call the select statement to get the records in the PRODUCTS class, and then loop and call the friend function in the PRODUCT class in the loop just passing that row, and in turn, fill the private vars of the PRODUCT class..

    sorry i know that probably sounds confusing...

    I guess I could either try nemaroller's suggestion using that System.Data.Common.DbDataRecord object, or I guess I could pass ByRef the reader each time and just load the record from the current position of the reader..

    which of those 2 would you guys suggest?

  12. #12
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    I would go with the pass byRef on the dreader first....

    Of course, it kinda does not make sense to pass a datareader to a 'Products' class... seems more you would have a SetValues method that took an object array...
    VB Code:
    1. While dr.Read()
    2.  Dim o() As Object
    3.  dr.GetValues(o)
    4.  CallAFunction(o)
    5. End While

  13. #13
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    All things considered, I'd pass the object byRef..... no compelling reason to do otherwise....

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width