2

I have my code setting up a parameter for QuestionsCount and then for Questions:

    parameterList.Add(new SqlParameter("@QuestionsCount", chunkSize));

    var p = new SqlParameter("@Questions", questions);
    p.TypeName = "dbo.QuestionList";
    parameterList.Add(p);

Is there a way that I can combine the last three lines and create a new SqlParameter with the typeName. I was looking at the definitions but cannot find one that takes the value (questions) and the TypeName "dbo.QuestionList".

1
  • I find that table valued parameters require (relatively) verbose setup before you can use them, so I just wrote an extension method: AddWithValue<T>(this SqlParameterCollection that, string parameterName, string typeName, IEnumerable<T> rows) that handles setting the type etc. plus empty IEnumerable<T>s get passed down as null (why that's a requirement I have no idea...) Commented Jul 20, 2014 at 7:20

2 Answers 2

3

There are quite a few overloads for the constructor for SqlParameter, some of which allow you to specify a datatype and optionally a length:

var p = new SqlParameter("@Questions", SqlDbType.VarChar, 100);
p.Value = ":.......";
parameterList.Add(p);

Check out the MSDN documentation on SqlParameter for details.

Sign up to request clarification or add additional context in comments.

Comments

1

One method is with an initializer to supply property values not available in the constructor overloads:

parameterList.Add(new SqlParameter( "@Questions", SqlDbType.Structured ) { TypeName = "dbo.QuestionList", Value = questions });

Comments

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.