Anyone know why the following is not working?
Dim strPhrase As String = "'%office%'"
objStringBuilder = New StringBuilder()
objSQLConnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("connString"))
objSQLCommand = New SqlCommand("select col1, col2 from table1 where phrase like @phrase", objSQLConnection)
objSQLCommand.Parameters.Add("@phrase", SqlDbType.VarChar, 255).Value = strPhrase
objSQLCommand.Connection.Open()
objSQLDataReader = objSQLCommand.ExecuteReader()
While objSQLDataReader.Read()
objStringBuilder.Append(objSQLDataReader("col1") & " - " & objSQLDataReader("col2"))
End While
objSQLDataReader.Close()
objSQLCommand.Connection.Close()
return objStringBuilder.tostring()
If I remove anything to do with phrase, it starts working again, i.e:
objStringBuilder = New StringBuilder()
objSQLConnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("connString"))
objSQLCommand = New SqlCommand("select col1, col2 from table1", objSQLConnection)
objSQLCommand.Connection.Open()
objSQLDataReader = objSQLCommand.ExecuteReader()
While objSQLDataReader.Read()
objStringBuilder.Append(objSQLDataReader("col1") & " - " & objSQLDataReader("col2"))
End While
objSQLDataReader.Close()
objSQLCommand.Connection.Close()
return objStringBuilder.tostring()
I get no errors, it just returns a blank string.
Remove the ” from around your search phrase, the command object deals with all that for you. i.e.