Solved

Multiple parameters in a Select statement at run time

Posted on 2006-07-13
4
258 Views
Last Modified: 2010-04-16
What is the best way to format a Select statement when the amount of parameter variables are not known until run time?
0
Comment
Question by:fly412s
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 4

Expert Comment

by:DarkXiphoid
ID: 17103039
Make an arraylist with parameters. Like this string[] paramList = new string[]

That way you can add any number of parameters during runtime.

Hope this helps you in the right direction.

Regards,

Robert
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 17103641
Are you talking about a SQL SELECT statement?  AFAIK it's not possible.  The best way I've seen is to use a finite set of VARCHAR parameters:

CREATE PROCEDURE MyParameterHack
@param1 VARCHAR(500) = null,
@param2 VARCHAR(500) = null,
@param3 VARCHAR(500) = null,
@param4 VARCHAR(500) = null,
@param5 VARCHAR(500) = null,
@param6 VARCHAR(500) = null
AS
...

However, SQL does support "optional" parameters, which may be a better option.

What is the procedure going to do?  Maybe there's an alternate method than variable parameters



0
 

Author Comment

by:fly412s
ID: 17103700
I'm using ADO.net.  I'm using the System.Data.OleDb namespace.  I'm connecting to a flat database file (.csv).  
0
 
LVL 25

Accepted Solution

by:
dstanley9 earned 500 total points
ID: 17103811
Then you can build it using string concatenation.

For example, to use a dynamic WHERE clause, use arrays for field names and values:

string[] fields;
string [] values;

// build arrays of fields and values

string sql = "SELECT * FROM flatfile WHERE " 
for(int i=0; i<numParams; i++)
{
  sql += "[" + fields[i] + "] = @Param" + i.ToString() ;
  if (i<numparams-1)
    sql += " AND ";
}
OleDbCommand command = new OleDbCommand(sql);

for(int i=0; i<numParams; i++)
{
  command.Parameters.Add("@Param" + i.ToString(),values[i]);
}



0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question