SQLCommand with Optional Parameters?

H

Hanover

In at typical SQL command if I want an optional parameter, I say

"Where ID=@ID or @ID=Null"

This way if I dont pass in a parameter, it will just pull all of the
IDs.

Is there a way to set this up in the text command of a SQLCommand
object in .NET?

I've tried, but it keeps throwing an error that tells me that it
expects the parameter. I have three possible optional parameters..and
I'd hate to do a select for every single combination.

Thanks,
Dan
 
R

rviray

could you use...

if(DoesItNeedToBeNull())
{
parms[x] = DBNull.Value; //DBNull is off the System Object
}

I am assuming that you are assigning your params....this way...

If not, your second alternative is building the entire SQL statemen
string like

string qInfo = "Select * From Tbl where "
if(sometest)
{
qInfo = qInfo + "blah = 'blahh'";
}
etc..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top