How to set TypeName in the constructor of new SqlParameter?

Total Post:96

 1342  View(s)
Rate this:

I am using the following code to set up parameters for a call to a stored procedure:

        List<SqlParameter>parameterList = new List<SqlParameter>();
        parameterList.Add(new SqlParameter("@Title", adminTest.Title));
        parameterList.Add(new SqlParameter("@Text", adminTest.Text));
        var questionsList = new SqlParameter("@Questions", questions);
        questionsList.TypeName = "dbo.QuestionList";

The code snippet works but what I would like to know is if anyone found a way to set the TypeName in the new SqlParameter constructor? I tried looking at the documentation but the only thing I can find is adding the typename afterwards.

  1. Post:67

    Re: How to set TypeName in the constructor of new SqlParameter?

    You can accomplish this with initializers. The example below specifies strongly-typed parameters and max length of variable values, which is a good practice from a SQL performance perspective.

        List<SqlParameter>parameterList = new List<SqlParameter>()
                new SqlParameter("@Title",SqlDbType.VarChar) {Size = 30, Value = adminTest.Title},
                new SqlParameter("@Text",SqlDbType.VarChar) {Size = 30, Value = adminTest.Text},
                new SqlParameter("@Questions",SqlDbType.Structured) {TypeName = "dbo.QuestionList", Value = questions}

      Modified On Apr-06-2018 11:53:25 PM