Results 1 to 6 of 6

Thread: Find a record in a datareader

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    784

    Find a record in a datareader

    Is it possible for me to find a record in a datareader .. I use below code .. (I don't want to use "Where clause in my SQL statement")

    Dim mKode as string
    Dim mSQLCheckData As String = "Select * from MyTable01 Order by Kode"
    Dim DataReader As SqlDataReader

    Dim SQLCommand As New SqlCommand(mSQLCheckData, SqlConnection1)

    SqlConnection1.Open()
    DataReader = SQLCommand.ExecuteReader()

    --
    --
    --

    SqlConnection1.Close()

    please help

    TIA

    Winanjaya

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Why would you not want to use a WHERE clause? If you don't then it is just being unefficent because you have to keep returning rows until you find the one you want otherwise the only one returned would be the one you want. This adds network traffic also.

  3. #3
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    Also, Select * is usually frowned upon, instead you might want to just return the columns you need (unless of course you need all of them )

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    784
    Hi All,

    As far as I know, in VB6 application, we can use find method in the recordset to find a record .. so is that mean that it is unavailble in
    VB.NET ?

    example:

    mSQL= "Select * from Student Order by StudentCode"

    rs.open msql,myconn,.. blah blah

    rs.find "studentcode = ' "& mycode & " ' "

    please advise .. thanks a lot in advance

    regards
    Winanjaya

  5. #5
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    USE A WHERE CLAUSE!!

    OR
    Use a dataset instead of datareader. Then you can create a dataview that will filter the objects.

  6. #6
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    if you really just want to load the dataset and then interrogate it, then try this:

    Dim tb2 As DataTable = ds2.Tables("Mytable")
    Dim found As Boolean = False
    Dim myRows() As DataRow
    Dim selec As String = "Mykey = " & CStr(MyKey)
    tb2 = ds2.Tables("Mytable")
    myRows = tb2.Select(selec)
    found = False
    Dim i As Integer
    For i = 0 To myRows.GetUpperBound(0)
    found = True
    ' Exit For
    Next i
    if found then
    dim dr2 as datarow = myRows(0)
    .........
    ...........

    end if

    not great code but should give you an idea.

    The underlying theme is to fill a dataset with records, then declare and use a datatable and its selec function to find a particular record that you want.


    There are a few more examples in the msdn help as well

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