471,348 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

Parameter list

Hi,

Is there a way to add a parameter list with only one statement as opposed to
having one statement for each parameter. For example if I have 20 parameters
(@p1, @p2 .... @p20) that I want to add to my parameter collection, could I
do it all in one statement more or less?

TIA
Roy
Dec 6 '05 #1
8 1619
Could you make it more clear.
If you are saying about collection, many classes expose AddRange
method.
Note that Array also implement ICollection.

Dec 6 '05 #2
I believe that the OP is referring to SQL parameters.

Dec 6 '05 #3
Ah yes, I didn't notice that.
So he just needs to write a helper method to do that.

Dec 6 '05 #4
Hi Truong,

What I mean is this. When I first add my parameters I do it like this (see
below), so I was wandering if there was a way to do this all in one
statement rather than 10 statements. I guess I could use a for loop and a
string builder to do it, but I was wandering if there was an inherent way to
do it.
oCommand.Parameters.AddWithValue("@par0", laEnum2Bk[0]);
oCommand.Parameters.AddWithValue("@par1", laEnum2Bk[1]);

oCommand.Parameters.AddWithValue("@par2", laEnum2Bk[2]);

oCommand.Parameters.AddWithValue("@par3", laEnum2Bk[3]);

oCommand.Parameters.AddWithValue("@par4", laEnum2Bk[4]);

oCommand.Parameters.AddWithValue("@par5", laEnum2Bk[5]);

oCommand.Parameters.AddWithValue("@par6", laEnum2Bk[6]);

oCommand.Parameters.AddWithValue("@par7", laEnum2Bk[7]);

oCommand.Parameters.AddWithValue("@par8", laEnum2Bk[8]);

oCommand.Parameters.AddWithValue("@par9", laEnum2Bk[9]);

oCommand.Parameters.AddWithValue("@par10", laEnum2Bk[10]);

Thanks

Roy

"Truong Hong Thi" <th*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Could you make it more clear.
If you are saying about collection, many classes expose AddRange
method.
Note that Array also implement ICollection.

Dec 6 '05 #5
As Truong Hong Thi said, a helper function is probably the way to go... I'm
assuming (from the AddWithValue method) that the command is a SqlCommand,
not an IDbCommand, so how about the following:

public static void AddCommandParams(SqlCommand command, string prefix,
params object[] values) {
int index = 0;
foreach (object value in values) {
command.Parameters.AddWithValue(prefix+(index++).T oString(),value);
}
}

Then call
AddCommandParams(oCommand, "@par", laEnum2Bk);

The "params" is so that you could also call:
AddCommandParams(oCommand, "@par", "MyValue", 1, 0.2);

Haven't tested the code, but looks like it should work (famous last
words...)

Marc

"Roy Gourgi" <ro***@videotron.ca> wrote in message
news:VK*****************@wagner.videotron.net...
Hi Truong,

What I mean is this. When I first add my parameters I do it like this (see
below), so I was wandering if there was a way to do this all in one
statement rather than 10 statements. I guess I could use a for loop and a
string builder to do it, but I was wandering if there was an inherent way
to do it.
oCommand.Parameters.AddWithValue("@par0", laEnum2Bk[0]);
oCommand.Parameters.AddWithValue("@par1", laEnum2Bk[1]);

oCommand.Parameters.AddWithValue("@par2", laEnum2Bk[2]);

oCommand.Parameters.AddWithValue("@par3", laEnum2Bk[3]);

oCommand.Parameters.AddWithValue("@par4", laEnum2Bk[4]);

oCommand.Parameters.AddWithValue("@par5", laEnum2Bk[5]);

oCommand.Parameters.AddWithValue("@par6", laEnum2Bk[6]);

oCommand.Parameters.AddWithValue("@par7", laEnum2Bk[7]);

oCommand.Parameters.AddWithValue("@par8", laEnum2Bk[8]);

oCommand.Parameters.AddWithValue("@par9", laEnum2Bk[9]);

oCommand.Parameters.AddWithValue("@par10", laEnum2Bk[10]);

Thanks

Roy

"Truong Hong Thi" <th*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Could you make it more clear.
If you are saying about collection, many classes expose AddRange
method.
Note that Array also implement ICollection.


Dec 6 '05 #6
Hi Marc,

I tried this and it seems to work fine for my needs. BTW would this be
faster if I used a string builder?

for (lpMisc = 1; lpMisc < 3; lpMisc++)

{

strC = "@par"+lpMisc.ToString();

oCommand.Parameters.AddWithValue(strC, laEnum2Bk[lpMisc]);

}

Thanks
Roy

"Marc Gravell" <mg******@rm.com> wrote in message
news:%2********************@tk2msftngp13.phx.gbl.. .
As Truong Hong Thi said, a helper function is probably the way to go...
I'm assuming (from the AddWithValue method) that the command is a
SqlCommand, not an IDbCommand, so how about the following:

public static void AddCommandParams(SqlCommand command, string prefix,
params object[] values) {
int index = 0;
foreach (object value in values) {

command.Parameters.AddWithValue(prefix+(index++).T oString(),value);
}
}

Then call
AddCommandParams(oCommand, "@par", laEnum2Bk);

The "params" is so that you could also call:
AddCommandParams(oCommand, "@par", "MyValue", 1, 0.2);

Haven't tested the code, but looks like it should work (famous last
words...)

Marc

"Roy Gourgi" <ro***@videotron.ca> wrote in message
news:VK*****************@wagner.videotron.net...
Hi Truong,

What I mean is this. When I first add my parameters I do it like this
(see below), so I was wandering if there was a way to do this all in one
statement rather than 10 statements. I guess I could use a for loop and a
string builder to do it, but I was wandering if there was an inherent way
to do it.
oCommand.Parameters.AddWithValue("@par0", laEnum2Bk[0]);
oCommand.Parameters.AddWithValue("@par1", laEnum2Bk[1]);

oCommand.Parameters.AddWithValue("@par2", laEnum2Bk[2]);

oCommand.Parameters.AddWithValue("@par3", laEnum2Bk[3]);

oCommand.Parameters.AddWithValue("@par4", laEnum2Bk[4]);

oCommand.Parameters.AddWithValue("@par5", laEnum2Bk[5]);

oCommand.Parameters.AddWithValue("@par6", laEnum2Bk[6]);

oCommand.Parameters.AddWithValue("@par7", laEnum2Bk[7]);

oCommand.Parameters.AddWithValue("@par8", laEnum2Bk[8]);

oCommand.Parameters.AddWithValue("@par9", laEnum2Bk[9]);

oCommand.Parameters.AddWithValue("@par10", laEnum2Bk[10]);

Thanks

Roy

"Truong Hong Thi" <th*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Could you make it more clear.
If you are saying about collection, many classes expose AddRange
method.
Note that Array also implement ICollection.



Dec 6 '05 #7
Re StringBuilder, probably not;

I'm going to take a liberty here and direct you at Jon Skeet's excellent
page on the subject (http://www.yoda.arachsys.com/csharp/stringbuilder.html)
(I hope you don't mind, Jon)

If you were constantly adding to a single string (i.e you were building up
some uber-command as a string (or a CSV or something) by enumerating through
a large-ish array) then maybe - but you're not. For concatenating 2 strings,
the + operator is almost certainly faster by a mile.

As a minor note, I'm sure your underlying code deals with this, but remember
that the array is 0-based (your code enumerates from 1, which is perfectly
valid, but sometimes a bug).

Marc

"Roy Gourgi" <ro***@videotron.ca> wrote in message
news:uI******************@wagner.videotron.net...
Hi Marc,

I tried this and it seems to work fine for my needs. BTW would this be
faster if I used a string builder?

for (lpMisc = 1; lpMisc < 3; lpMisc++)

{

strC = "@par"+lpMisc.ToString();

oCommand.Parameters.AddWithValue(strC, laEnum2Bk[lpMisc]);

}

Thanks
Roy

"Marc Gravell" <mg******@rm.com> wrote in message
news:%2********************@tk2msftngp13.phx.gbl.. .
As Truong Hong Thi said, a helper function is probably the way to go...
I'm assuming (from the AddWithValue method) that the command is a
SqlCommand, not an IDbCommand, so how about the following:

public static void AddCommandParams(SqlCommand command, string prefix,
params object[] values) {
int index = 0;
foreach (object value in values) {

command.Parameters.AddWithValue(prefix+(index++).T oString(),value);
}
}

Then call
AddCommandParams(oCommand, "@par", laEnum2Bk);

The "params" is so that you could also call:
AddCommandParams(oCommand, "@par", "MyValue", 1, 0.2);

Haven't tested the code, but looks like it should work (famous last
words...)

Marc

"Roy Gourgi" <ro***@videotron.ca> wrote in message
news:VK*****************@wagner.videotron.net...
Hi Truong,

What I mean is this. When I first add my parameters I do it like this
(see below), so I was wandering if there was a way to do this all in one
statement rather than 10 statements. I guess I could use a for loop and
a string builder to do it, but I was wandering if there was an inherent
way to do it.
oCommand.Parameters.AddWithValue("@par0", laEnum2Bk[0]);
oCommand.Parameters.AddWithValue("@par1", laEnum2Bk[1]);

oCommand.Parameters.AddWithValue("@par2", laEnum2Bk[2]);

oCommand.Parameters.AddWithValue("@par3", laEnum2Bk[3]);

oCommand.Parameters.AddWithValue("@par4", laEnum2Bk[4]);

oCommand.Parameters.AddWithValue("@par5", laEnum2Bk[5]);

oCommand.Parameters.AddWithValue("@par6", laEnum2Bk[6]);

oCommand.Parameters.AddWithValue("@par7", laEnum2Bk[7]);

oCommand.Parameters.AddWithValue("@par8", laEnum2Bk[8]);

oCommand.Parameters.AddWithValue("@par9", laEnum2Bk[9]);

oCommand.Parameters.AddWithValue("@par10", laEnum2Bk[10]);

Thanks

Roy

"Truong Hong Thi" <th*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Could you make it more clear.
If you are saying about collection, many classes expose AddRange
method.
Note that Array also implement ICollection.



Dec 6 '05 #8
Thanks Marc.

Roy

"Marc Gravell" <mg******@rm.com> wrote in message
news:uB****************@TK2MSFTNGP15.phx.gbl...
Re StringBuilder, probably not;

I'm going to take a liberty here and direct you at Jon Skeet's excellent
page on the subject
(http://www.yoda.arachsys.com/csharp/stringbuilder.html) (I hope you don't
mind, Jon)

If you were constantly adding to a single string (i.e you were building up
some uber-command as a string (or a CSV or something) by enumerating
through a large-ish array) then maybe - but you're not. For concatenating
2 strings, the + operator is almost certainly faster by a mile.

As a minor note, I'm sure your underlying code deals with this, but
remember that the array is 0-based (your code enumerates from 1, which is
perfectly valid, but sometimes a bug).

Marc

"Roy Gourgi" <ro***@videotron.ca> wrote in message
news:uI******************@wagner.videotron.net...
Hi Marc,

I tried this and it seems to work fine for my needs. BTW would this be
faster if I used a string builder?

for (lpMisc = 1; lpMisc < 3; lpMisc++)

{

strC = "@par"+lpMisc.ToString();

oCommand.Parameters.AddWithValue(strC, laEnum2Bk[lpMisc]);

}

Thanks
Roy

"Marc Gravell" <mg******@rm.com> wrote in message
news:%2********************@tk2msftngp13.phx.gbl.. .
As Truong Hong Thi said, a helper function is probably the way to go...
I'm assuming (from the AddWithValue method) that the command is a
SqlCommand, not an IDbCommand, so how about the following:

public static void AddCommandParams(SqlCommand command, string prefix,
params object[] values) {
int index = 0;
foreach (object value in values) {

command.Parameters.AddWithValue(prefix+(index++).T oString(),value);
}
}

Then call
AddCommandParams(oCommand, "@par", laEnum2Bk);

The "params" is so that you could also call:
AddCommandParams(oCommand, "@par", "MyValue", 1, 0.2);

Haven't tested the code, but looks like it should work (famous last
words...)

Marc

"Roy Gourgi" <ro***@videotron.ca> wrote in message
news:VK*****************@wagner.videotron.net...
Hi Truong,

What I mean is this. When I first add my parameters I do it like this
(see below), so I was wandering if there was a way to do this all in
one statement rather than 10 statements. I guess I could use a for loop
and a string builder to do it, but I was wandering if there was an
inherent way to do it.
oCommand.Parameters.AddWithValue("@par0", laEnum2Bk[0]);
oCommand.Parameters.AddWithValue("@par1", laEnum2Bk[1]);

oCommand.Parameters.AddWithValue("@par2", laEnum2Bk[2]);

oCommand.Parameters.AddWithValue("@par3", laEnum2Bk[3]);

oCommand.Parameters.AddWithValue("@par4", laEnum2Bk[4]);

oCommand.Parameters.AddWithValue("@par5", laEnum2Bk[5]);

oCommand.Parameters.AddWithValue("@par6", laEnum2Bk[6]);

oCommand.Parameters.AddWithValue("@par7", laEnum2Bk[7]);

oCommand.Parameters.AddWithValue("@par8", laEnum2Bk[8]);

oCommand.Parameters.AddWithValue("@par9", laEnum2Bk[9]);

oCommand.Parameters.AddWithValue("@par10", laEnum2Bk[10]);

Thanks

Roy

"Truong Hong Thi" <th*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
> Could you make it more clear.
> If you are saying about collection, many classes expose AddRange
> method.
> Note that Array also implement ICollection.
>



Dec 7 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Michael Birkmose | last post: by
3 posts views Thread by Joe | last post: by
3 posts views Thread by tshad | last post: by
9 posts views Thread by TheFlyingDutchman | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.