Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Basically what I am trying to achieve is the proper select statement to search each field or column in my access database table.

VB.NET
Public Shared Function Access_Find_Match(ByVal MatchString As String) As Boolean
        Dim Access_Command As New OleDbCommand("SELECT * FROM Table WHERE Field0 = '" & MatchString & "'" Or "Field1 = '" & MatchString & "'" Or "Field2 = '" & MatchString & "'", Access_Database)
        Using Access_Datareader As OleDbDataReader = Access_Command.ExecuteReader
            Return CStr(Access_Datareader.Read)
        End Using
    End Function



I get a error when using the "Or" to try and search each field

C#
Conversion from string "SELECT * FROM Table WHERE Field0" to type 'Long' is not valid.


If I change the "Or" to "&" I get the error

C#
Syntax error (missing operator) in query expression 'Field0= 'MatchString'Field1= 'MatchString'field3= 'MatchString'.


what would be the correct SELECT string to use in this case or what error exists in the syntax?, is the error possibly caused by the return type of the function?

Any help with this is greatly appreciated!
Posted
Updated 11-Jan-16 20:16pm
v2

The SQL Query should be like

SQL
SELECT * 
FROM Table WHERE Field0 = '<somestringvalue>'
OR Field1 = '<somestringvalue>'
OR Field2 = '<somestringvalue>'</somestringvalue></somestringvalue></somestringvalue>


Your code should look like

VB
Dim Access_Command As New OleDbCommand(String.Format("SELECT * FROM Table WHERE Field0 = '{0)' OR Field1 = '{0} OR FIELD2 = '{0}'", MatchString), Access_Database);


However the above approach is error prone to SQL Injection Attacks, you need to parameterize your query.
 
Share this answer
 
try with below,
VB
Dim Access_Command As New OleDbCommand("SELECT * FROM Table WHERE Field0 = '" & MatchString & "' Or Field1 = '" & MatchString & "' Or Field2 = '" & MatchString & "'", Access_Database)

You better use sql parameters to avoid sql injection attacks. then,
VB
 Dim Access_Command As New OleDbCommand("SELECT * FROM Table WHERE Field0 = ? Or Field1 = ? Or Field2 = ?", Access_Database)
Access_Command.Parameters.AddWithValue("p1",MatchString)
Access_Command.Parameters.AddWithValue("p2",MatchString)
Access_Command.Parameters.AddWithValue("p3",MatchString)
 
Share this answer
 
v2
Comments
Draco2013 12-Jan-16 3:01am    
Thank you very much this was exactly the answer, also thank you asif for your answer I believe it to work but went with the second answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900