?
Solved

Multiple parameters in a Select statement at run time

Posted on 2006-07-13
4
Medium Priority
?
266 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
  • 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 2000 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

850 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