I have this query (not important per se):
SqlCommand sqlCmd = new SqlCommand("SELECT * FROM Table1 WHERE " +
"CONTAINS((col1, col2, col3), " + "'\"*m*\"' )" +
"ORDER BY(SELECT null) " +
"OFFSET(1) ROWS FETCH NEXT(100) ROWS ONLY", conn);
It returns all rows that have an 'm' in the specified columns, as it should. Notice that the 'm' is inside a pair of asterisks (wild characters), then quotation marks, then apostrophes.
I wanted to parametrize it, i.e. put any string in the query.
I wrote this:
SqlCommand sqlCmd = new SqlCommand("SELECT * FROM Table1 WHERE "+ "CONTAINS((col1, col2, col3), " + "'\"*@searchText*\"' )" +
"ORDER BY(SELECT null) "+
"OFFSET(1) ROWS FETCH NEXT(100) ROWS ONLY", conn);
sqlCmd.Parameters.AddWithValue("@searchText", textToSearch);
But instead of putting the textToSearch string's contents in the SqlCommand, this code puts @searchText itself.
I looked similar posts here and tried to follow but it did not work, probably because there is an apostrophe and a quotation mark and an asterisk in the format.
What am I doing wrong?
How should I specify this command?
@searchTextin*? Sorry if this is obvious, I don't claim to know everything.CONTAINS((col1, col2, col3), '"*@searchText*"')in your string.