Monday, March 7, 2011

How can I add non-column specific parameters to a C# table adapter?

I'm using a TableAdapter for the first time and adding a custom query to it, and I'm getting stuck on adding some search parameters to my query, here's what I've got:

SELECT  *
FROM    Orders
WHERE   (id_order = @id_order) OR
        (IsFor LIKE '%@word1%') OR
        (IsFor LIKE '%@word2%') OR
        (IsFrom LIKE '%@word1%') OR
        (IsFrom LIKE '%@word2%')

When I test execute the query, I'm prompted for id_order, but not word1 or word2. I also tried adding these directly as parameters to the adapter and pass them in but they don't work. Strangely, id_order continues to work, but the other values don't generate any matches.

My goal is to allow the user to type in first &/or last name and have it match any orders with that first &/or last name.

Any ideas as to what I'm doing wrong?

From stackoverflow
  • You don't need quotes or percent signs.

    SELECT  *
    FROM    Orders
    WHERE   (id_order = @id_order) OR
        (IsFor LIKE @word1) OR
        (IsFor LIKE @word2) OR
        (IsFrom LIKE @word1) OR
        (IsFrom LIKE @word2)
    
    Zartog : That was definitely half of it. I still did need the % signs for a partial match, but instead of embedding them in the query, I added these to the parameters directly and voila, it worked. Thanks!
    recursive : My bad. I thought the % was already there. If you want to add them in the query, do IsFor LIKE '%' + @word1 + '%'

0 comments:

Post a Comment