0

I am wondering how (in my code below), that I make sure that dataID is a text (or varchar or int) in the parameter passing below?

public T ExecuteQuery<T>(Func<IDataReader, T> getResult, string query, params IDataParameter[] parameters)
    {
        using (SqlConnection conn = new SqlConnection(this.DefaultConnectionString))
        {
            conn.Open();

            // Declare the parameter in the query string
            using (SqlCommand command = new SqlCommand(query, conn))
            {
                foreach (var parameter in parameters)
                {
                    command.Parameters.Add(parameter);
                }

                command.Prepare();

                using (SqlDataReader dr = command.ExecuteReader())
                {
                    return getResult(dr);
                }
            }
        }
    }

    public string GetMySpecId(string dataId)
    {
        return ExecuteQuery(
            dr =>
            {
                if (dr.Read())
                {
                    return dr[0].ToString();
                }

                return string.Empty;
            },
            "select specId from MyTable where dataId = @dataId",
            new SqlParameter("dataId", dataId));
    }

I am used to code like so:

command.Parameters.Add(new SqlParameter("key", SqlDbType.Text));

command.Prepare();

command.Parameters[0].Value = dataId;

How else in the code at the top, do I pass two or more parameters into the ExecuteQuery?

2 Answers 2

2

ExecuteQuery already takes multiple parameters. It is a ParamArray which means it takes any number of parameters and turns them into an array.

For example, if I have a function like this:

public int Sum(params int[] numbersToSum) { ... }

I can call it like this:

Sum(1, 2, 5, 6, 1)

In your case you can call it this way:

ExecuteQuery(
   dr => { //snip }, 
   query,
   new SqlParameter("dataId", dataId),
   new SqlParameter("anotherParm", parm2),
   new SqlParameter("anotherParm", parm3),
   ... );
Sign up to request clarification or add additional context in comments.

2 Comments

how do i make it so the type is passed too, like SqlDBType.NVarchar?
Assuming you're using the the SqlParameter class in System.Data.SqlClient, that's something that you can set on the SqlParameter objects you are passing (the SqlDBType property). However, the SqlDBType is inferred from the .NET object given in the constructor so you don't really need to specify it: msdn.microsoft.com/en-us/library/0881fz2y.aspx
1

The code is using the params keyword, so it seems that you should just write additional escaped SQL and keep appending the parameters to the end of the method. Here is an example using your code as a base.

The link provides a fuller explanation, but params makes it so that you can have a variable number of parameters (as long as they are the same type) at the end of a method signature. It puts all of the final parameters that fall in this params variable into an array.

 return ExecuteQuery(
        dr =>
        {
            if (dr.Read())
            {
                return dr[0].ToString();
            }

            return string.Empty;
        },
        "select specId from MyTable where dataId = @dataId and somethingelse = @else",
        new SqlParameter("dataId", dataId), 
        new SqlParameter("else", elseVar);

2 Comments

what is they are of different types?
i would also want to pass different types, maybe I should change it to a dictionary?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.