?
Solved

Multiple parameters in a Select statement at run time

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

762 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