static method to executenonquery with parameters

I have static methods in a class that i use and call like executesql which has
for example
 public void ExecuteSql(string sqlStr)
                using (SqlConnection con = new SqlConnection(globalconnStr))
                using (SqlCommand cmd = new SqlCommand(sqlStr, con))
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = sqlStr;
            catch (SqlException exExecuteSql)
                throw exExecuteSql;

Open in new window

I was wondering if there is a way to pass parameters or any number of parameters
and the sql query or how would I go about doing that?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
The code you have posted is not a static method.

To your question, "I was wondering if there is a way to pass parameters or any number of parameters ", you could make one of the parameters in the signature of the function and object array and pass any number of values. Or if you are talking about optional parameters please have a look at this, Named and Optional Arguments (C# Programming Guide).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jacques Bourgeois (James Burger)PresidentCommented:
You might give a look at the params keyword in the documentation. It enables you to create methods that takes a variable number of arguments.

You could then loops through the parameters passed to your params argument and create the necessary SqlParameters collection to pass the parameters.

Another solution would be to concatenate the values of your parameters in your SQL command before passing it to your method.
r3nderAuthor Commented:
Thanks guys
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.