• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

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?
2 Solutions
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).
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now