2

I have this part of code

string query = "SELECT ID, COL1 FROM TABLE1 WHERE CONTAINS(COL1,@text)";

sqlCommand.CommandText = sql;
sqlCommand.Parameters.Add("@text", value+"*");

value is a function parameter.

For fulltext search, the sql statement must be like this:

SELECT ID, COL1 FROM TABLE1 WHERE CONTAINS(COL1,'"eng*"')

It could search strings which start with "eng" -> english, bla blah.

But executing in C# the above code then ExecuteReader() returns empty list.

@text has as value "sometext*" but I want to add ' ' characters.

I tried string query = "SELECT ID, COL1 FROM TABLE1 WHERE CONTAINS(COL1,'@text')"; but it doesn't work, returns empty list.

Why ? How do I proceed that @text parameters must include '' characters over value for search ?

Thank you

Snake Eyes
  • 16,287
  • 34
  • 113
  • 221
  • 1
    If you read your own question as if you hadn't wrote it, do you understand it? I am thinking about for example *"It could search strings which start with "eng" -> english, bla blah."* - It is not clear what you mean here. *"@text has as value "value*" but I want to add ' ' characters."* - What should be the end result of this, `value* `? or perhaps `value*' '`? *"but it doesn't work !"*, why does it not work? error messages? does it crash? what happends? *"How do I proceed that @text must include '' characters over value for search ?"* So text should be `value*''`? Please try to be more clear. – default Feb 22 '12 at 09:47

2 Answers2

0

Have you tried specifying the data type? I don't think you should need the quotes in the sqlcommand string.

sqlCommand.Parameters.Add("@text", SqlDbType.VarChar);
sqlCommand.Parameters["@text"].Value = value+"*";

Or SqlDbType.NVarChar.

Nanhydrin
  • 4,332
  • 2
  • 38
  • 51
0

Can you try using sqlCommand.Parameters.AddWithValue("@text", value+"*");

If above doesn't work, you can use SQL Parameter and SqlDBType.

I think your SQL query is wrong. See is this post helps.

C# constructing parameter query SQL - LIKE %

Community
  • 1
  • 1
Junaid
  • 1,708
  • 16
  • 25