Results 1 to 8 of 8

Thread: Can I search a DataReader? (RESOLVED)

  1. #1

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200

    Question Can I search a DataReader? (RESOLVED)

    Based on the code:

    VB Code:
    1. Dim myConnection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=myDatabase.mdb")
    2. Dim myCommand As New OleDb.OleDbCommand("SELECT * FROM myTable WHERE Name='Alvaro'")
    3. Dim myDataReader As OleDb.OleDbDataReader
    4. myConnection.Open()
    5. myDataReader = myCommand.ExecuteReader
    Is there a way to search the records stored in myDataReader?
    Like:

    VB Code:
    1. myDataReader.Select("LastName='F1'")
    2. If myDataReader.NoMatch Then
    3.     MsgBox("Found.")
    4. Else
    5.     MsgBox("Not found.")
    6. End If
    Thanks.
    Last edited by AlvaroF1; Sep 16th, 2003 at 03:47 PM.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I did it different way . It may looks weird but for me , it get the job well done .
    VB Code:
    1. Public Shared Sub FineThis(ByVal MyTable As String, ByVal MyColumn As String, ByVal SearchWord As String)
    2.         Dim MyPath As String = Application.StartupPath & "\anyfile.mdb"
    3.         Dim MyConnection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & MyPath)
    4.  
    5.         Dim dr As OleDb.OleDbDataReader
    6. Dim SQLString As String = "SELECT " & MyColumn & " FROM " & MyTable & " WHERE " & MyColumn & "='" & SearchWord & "'"
    7.         Dim cmd As New OleDb.OleDbCommand(SQLString, MyConnection)
    8.  
    9.         MyConnection.Open()
    10.         Try
    11.             dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    12.             If dr.Read = True Then
    13.                 MessageBox.Show("Found :" & dr.Item(MyColumn))
    14.             Else
    15.                 MessageBox.Show("No Matches")
    16.             End If
    17.         Catch ex As Exception
    18.             MsgBox(ex.Message)
    19.         End Try
    20.         MyConnection.Close()
    21.     End Sub


    Usage :
    VB Code:
    1. FineThis("mytab", "c_url", TextBox1.Text)
    Last edited by Pirate; Sep 16th, 2003 at 11:23 AM.

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I don't think you can with a datareader. You can do a loop until you find the value you are looking for.

    Or
    You can return a dataset, then do a dataview on it to search it.

    Or
    With your query, why don't you get more accurate results back.
    Code:
    SELECT * FROM myTable WHERE Name='Alvaro' AND LastName='F1'
    If you get a result back, it is there (found). If not, then not found.

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    And btw , DataReader obj , doesn't store values in it . It's just a fast and forward way to read a single data and gets back to its bed . .

    hellswraith , did you test the code ?

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I know it's not the efficient way to do some search but at least this is what's he has been asking for .Check this demo :
    Attached Files Attached Files
    Last edited by Pirate; Sep 16th, 2003 at 11:24 AM.

  6. #6
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I didn't test it, don't have time right now.

    My thing is, why is he pulling data from the database that doesn't need to be pulled? Focusing your SQL Statement as much as possible to narrow the data returned is almost always the better bet.

    If you need the extra data for something else, then the datareader shouldn't be used because it can only be looked at once. The dataset would be the better option.

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I've changed the SQL String to select only specified column .

    VB Code:
    1. Dim SQLString As String = "SELECT " & MyColumn & " FROM " & MyTable & " WHERE " & MyColumn & "='" & SearchWord & "'"

    I agree , that dataset is best bet . This will be faster for some cases like :Checking username and password , checking if the entered data is already stored or not . This is how I use it usually .

  8. #8

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200
    Thank you all.

    Here's my code solution:

    VB Code:
    1. Dim myConnection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=myDatabase.mdb")
    2. Dim myAdapter As New OleDb.OleDbDataAdapter("SELECT * FROM myTable WHERE Name='Alvaro'", myConnection)
    3. Dim myDataSet As New DataSet()
    4. Dim myDataRow As DataRow()
    5. myAdapter.Fill(myDataSet, "myTable")
    6. myDataRow = myDataSet.Tables("myTable").Select("LastName='F1'")
    7. If myDataRow.Length = 0 Then
    8.     MsgBox("Not found.")
    9. Else
    10.     MsgBox("Found.")
    11. End If

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