1

for SqlCommand.Parameters.Add method overload including Size argument here is stipulated in MSDN:

"This overload is useful when you are adding a parameter of a variable-length data type such as varchar or binary."

and example is given:

public void AddSqlParameter(SqlCommand command) 
{
    SqlParameter param = new SqlParameter(
        "@Description", SqlDbType.NVarChar, 16);
    param.Value = "Beverages";
    command.Parameters.Add(param);
}

I have some difficulties to understand more exactly:

  1. What should be supplied in Size argument? Length of Column in DB structure or Length of current Parameter Value or Min() of them? Or something else?

  2. Is it available only for "varchar or binary" or for other literal Sql Server types too, e.g. char, nvarchar, etc..?

OR

EDITED: My concrete case: If in DB column name is varchar(10) but i have in C# String lName with Length of 15 what i have to put in Parameters.Add: 10 or lName.Length or ...?

1
  • 1
    You need to pass the length as '10' Commented Mar 1, 2013 at 11:26

1 Answer 1

1

The Size is inferred from the value of the dbType parameter if it is not explicitly set in the size parameter.

SqlParameter Constructor (String, SqlDbType, Int32)

The Size property is used for binary and string types. For parameters of type SqlType.String, Size means length in Unicode characters. For parameters of type SqlType.Xml, Size is ignored.

SqlParameter.Size Property

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

2 Comments

ok, still didn't understand it: if in DB column name is varchar(10) but i have in C# String lName with Length of 15 what i have to put in Parameters.Add: 10 or lname.Length?
Please check the "Remarks" part of SqlParameter.Size Property link in my answer

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.