views:

459

answers:

3

Suppose I have a search screen that is intended for looking up items. There are various optional search options on the screen that will cause the SQL query statement to vary.

Here are some example searches:

  1. Description search
  2. Description search + item supplier id
  3. Description search + item supplier id + item hierarchy level 1 id
  4. Description search + item supplier id + item hierarchy level 1 id + level 2 id
  5. item hierarchy level 1 id + level 2 id (no description, no item supplier id)

...you get the idea. There are quite a number of possible combinations. I was hoping to use parameterized queries for the performance benefits and such (plus I'm using them for the rest of the queries throughout the program).

Is there a way to do this or am I forced to either create each possible query and matching SQLiteCommand object or build the query string dynamically with a StringBuilder based on the options selected?

I'm using using the SQLite.NET data provider with C# 3.0 (on the 3.5 compact framework).


UPDATE

Based on some of the suggestions with null default values for the parameters and using (@param isnull or column = @param), I think I should be able to get this to work. I'll keep you posted.

NOTE: I am avoiding using stored procedures because the rest of the code uses parameterized queries instead of stored procedures. I'd like to keep everything consistent for the sanity of future maintenance programmers. It shouldn't make too much of a difference anyway.


UPDATE 2

This worked great on a desktop system (which is where I did my initial testing for the queries). However, it was very slow on the Windows CE Device I was using. Unusably slow. All the same, I can definitely use this in the future and it's very handy. Just not when running queries on a mobile device.

Thanks

A: 

Your probably best off creating stored procedures for each case.

If you have inline SQL code in your c# built with a StringBuilder then the execution plans will never be cached and it will not perform as well as it would with stored procedures.

DeletedAccount
This is why I was creating parameterized queries. They are also cached. I have a SQLiteCommand object with its own set of SQLiteParameter objects, so once the query is run it is supposed to be cached. The problem is that to use this approach I will need one for every possible search combination. I think using stored procedures I will run into the same issue... I'll need one for each possible search combination. This is what I'm trying to avoid.
Jason Down
does that cache execution plans too or is it just data?
DeletedAccount
I'm under the impression it's the entire execution plan (I could be wrong).
Jason Down
This is the best article I've found, but seems to be open to interpretation http://miguelbandera.wordpress.com/2008/04/03/consultas-parametrizadas-en-sqlite/
DeletedAccount
This is exactly what I'm trying to do, but do it with an arbitrary number of parameters. I think I have a way to do it now based on some of the ideas mentioned with stored procedures and default values. I'll see if I can emulate it by giving a default value of null to my SQLiteParameter objects.
Jason Down
best of luck :o)
DeletedAccount
Thanks Nath d:-D
Jason Down
+1  A: 

You can assign the parameters default values and handle the logic within your stored procedure:

create procedure mySproc(@description nvarchar(20) = null, @supplierid int = null, etc...)

handle the logic of whether the parameters are null in the body of the sproc.

jn29098
+3  A: 

From the stored procedure side you can default values to null then build your where clause to accommodate this null value.

ALTER Procedure FooProcedure 
    @SupplierID INT = NULL,
    @LevelOne INT = NULL 
AS
BEGIN

    SELECT SupplierID, LevelOne 
      FROM FooTable
     WHERE @SupplierID IS NULL OR SupplierID = @SupplierID
       AND @LevelOne IS NULL OR LevelOne = @LevelOne 

END
William Edmondson
This looks promising. Now I wonder if I can do the same in my c# code with a parameterized query rather than a stored procedure (I want to avoid SPs if I can because none of the other code is using them... consistency would be nice).
Jason Down
@Jason: For c#, I think you can use overloading in that case. You could use Nullable types for structs. VB.net has support for optional arguments (in which you could have 1 method with optional parameters as against overloading).
shahkalpesh
yep, this answer is rather standard approach when your search returns same data structure and accepts known set of optional parameters.
DK
If you have lots of parameters it is probably best to have a number of overloaded methods rather than one really big one. Easier to program against.
William Edmondson
@William That's probably the approach I'll take. Unless of course I was using c# 4.0 where I could then use default parameter values and named parameters!
Jason Down