How to specify Varchar size in AddInParameter

Topics: Data Access Application Block
Apr 18, 2011 at 5:06 PM

I am using GetSqlStringCommand, NOT procudere.

How can I specify the size of string?

Db.AddInParameter(command, "myvarchar", DbType.AnsiString, stringVariable);

Apr 19, 2011 at 12:12 AM

There's no overload of the AddInParameter which allows you to specify the size.  I'm not sure though why do you specifically need this since because you'll pass the actual value of the parameter anyway, it's not an out parameter. 

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 19, 2011 at 12:46 AM

Since I am using Adhoc queries, ADO.NET infers the varchar size depending of string that I use.

Then Sql Server create a new execution plan for each possible size.

Apr 19, 2011 at 1:06 AM

A way around would be to create a DbParameter through the CreateParameter method of a DbCommand object and adding it to the Parameters property of the same DbCommand object.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 19, 2011 at 10:57 AM

I created a extension method:

 

public static void AddInParameter(this Database banco, DbCommand command, string name, DbType dbType, object value, int size)

        {

            banco.AddInParameter(command, name, dbType, value);            command.Parameters["@" + name].Size = size;

        }

Apr 20, 2011 at 12:00 AM

That would be a better idea :)

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com