Sep 16th, 2003, 10:29 AM
#1
Thread Starter
Addicted Member
Can I search a DataReader? (RESOLVED)
Based on the code:
VB Code:
Dim myConnection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=myDatabase.mdb")
Dim myCommand As New OleDb.OleDbCommand("SELECT * FROM myTable WHERE Name='Alvaro'")
Dim myDataReader As OleDb.OleDbDataReader
myConnection.Open()
myDataReader = myCommand.ExecuteReader
Is there a way to search the records stored in myDataReader?
Like:
VB Code:
myDataReader.Select("LastName='F1'")
If myDataReader.NoMatch Then
MsgBox("Found.")
Else
MsgBox("Not found.")
End If
Thanks.
Last edited by AlvaroF1; Sep 16th, 2003 at 03:47 PM .
Sep 16th, 2003, 11:00 AM
#2
Sleep mode
I did it different way . It may looks weird but for me , it get the job well done .
VB Code:
Public Shared Sub FineThis(ByVal MyTable As String, ByVal MyColumn As String, ByVal SearchWord As String)
Dim MyPath As String = Application.StartupPath & "\anyfile.mdb"
Dim MyConnection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & MyPath)
Dim dr As OleDb.OleDbDataReader
Dim SQLString As String = "SELECT " & MyColumn & " FROM " & MyTable & " WHERE " & MyColumn & "='" & SearchWord & "'"
Dim cmd As New OleDb.OleDbCommand(SQLString, MyConnection)
MyConnection.Open()
Try
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
If dr.Read = True Then
MessageBox.Show("Found :" & dr.Item(MyColumn))
Else
MessageBox.Show("No Matches")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
MyConnection.Close()
End Sub
Usage :
VB Code:
FineThis("mytab", "c_url", TextBox1.Text)
Last edited by Pirate; Sep 16th, 2003 at 11:23 AM .
Sep 16th, 2003, 11:02 AM
#3
PowerPoster
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.
Sep 16th, 2003, 11:04 AM
#4
Sep 16th, 2003, 11:14 AM
#5
Sleep mode
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
Last edited by Pirate; Sep 16th, 2003 at 11:24 AM .
Sep 16th, 2003, 11:19 AM
#6
PowerPoster
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.
Sep 16th, 2003, 11:28 AM
#7
Sleep mode
I've changed the SQL String to select only specified column .
VB Code:
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 .
Sep 16th, 2003, 03:45 PM
#8
Thread Starter
Addicted Member
Thank you all.
Here's my code solution:
VB Code:
Dim myConnection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=myDatabase.mdb")
Dim myAdapter As New OleDb.OleDbDataAdapter("SELECT * FROM myTable WHERE Name='Alvaro'", myConnection)
Dim myDataSet As New DataSet()
Dim myDataRow As DataRow()
myAdapter.Fill(myDataSet, "myTable")
myDataRow = myDataSet.Tables("myTable").Select("LastName='F1'")
If myDataRow.Length = 0 Then
MsgBox("Not found.")
Else
MsgBox("Found.")
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
Forum Rules
Click Here to Expand Forum to Full Width