ALZ
ALZ

Reputation: 1998

ADO.NET: SqlParameterCollection.Add Method (String, SqlDbType, Int32). Size argument - what is meaning?

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 ...?

Upvotes: 1

Views: 1392

Answers (1)

andy
andy

Reputation: 6079

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

Upvotes: 1

Related Questions