Solved

Multiple parameters in a Select statement at run time

Posted on 2006-07-13
4
254 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Need help with a query 6 54
C# Application Local DB Connection String 23 57
Image(2) 3 22
dynamic menu in asp.net c# 11 26
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now