English 中文(简体)
using parameter in SQL with LIKE keyword
原标题:

from my C#-programm, I access a SQL Server 2008 Database. I have a table with a fulltextindex and want to search for an indexed entry:

SELECT page_id FROM page_categories WHERE page_title LIKE @title

When title has no withespaces everything is fine, but when title does contain withespaces, the request fails. It throws no error, but the returned recordset is closed

command.ChangeParameter("title", title);

using (System.Data.IDataReader reader = command.ExecuteQuery())
{
  while (reader.Read())
    resultSet.Add(reader.GetInt32(0));

  reader.Close();
}

I cannot enclose the @title parameter with , because then the Command-Object would interpret the @title as a string and not a parameter.

SELECT page_id FROM page_categories WHERE page_title LIKE  @title 

Anyone knows how I can use a parameter for the LIKE request when the title-parameter can contain whitespace?

Thanks in advance, Frank

问题回答

You can do this:

SELECT page_id 
FROM page_categories 
WHERE page_title LIKE  %  + @title +  % 

alternatively to the ck s answer, you could do this:

command.ChangeParameter("title", "%"+title.Trim()+"%");

You don t use LIKE to search a freetext index. You use CONTAINS or FREETEXT or similar.

SELECT page_id 
FROM page_categories 
WHERE page_title LIKE  %  + @title +  % 

The % represents a wild char similar to the more common * outside SQL (Any string of zero or more characters).

You should make sure that in the calling code the parameter is trimmed via .Trim().

Here is the full reference of possible wildchars.

I add the single quotes to the query:

SELECT page_id FROM page_categories WHERE page_title LIKE    + @title +   

If I try this with Contains ( CONTAINS (page_title, + @tile + ) ), I get a syntax error.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签