Searching a database for a string in a field

Aaronsmity

New member
Joined
Nov 18, 2006
Messages
2
Programming Experience
Beginner
dim blnFound as boolean
dim dr as datarow
ForEach dr In ds.Tables("dtCustomers").Rows
If dr("Phone").Trim = txtPhone.Text.Trim Then
blnFound = True
intCustID = dr("CustID")
MessageBox.Show("Found")
Else
blnFound = False
MessageBox.Show("Not Found")
EndIf
Next

Any ideas why I always get back a not found on strings that are definitely in the database? the table is filled, i dont know what the problem is any ideas? thanks, this is in vb.net
 
This code does not search the database, it searches the results pulled from the database into the client. Is this what you intended? If possible you should use the database search functionality - i.e. the notion that Microsoft/Oracle/IBM spent years and millions making their DB have a great search probably means you or I cant really do a better job by taking 5 minutes out to reinvent their wheel using a for loop and some string comparison..

Index the phone field in the DB and fire off a scalar query of "SELECT COUNT(*) FROM customers WHERE phonenum = @phonenum" (where the @phonenum parameter will be filled by the number)
I guarantee this will operate quicker and be safer for multi user use, than that for loop
 
Back
Top