Solved

Setting the paramter size for a SqlParameter

Posted on 2007-11-29
4
220 Views
Last Modified: 2010-04-21
I have this question about whether or not it's prefered to set the size of a SqlParamter.
I usually do something like this:
SqlCommand cmd = new SqlCommand(StoredProcName,Connection);
cmd.Parameters.Add("@SomeParamter", SqlDbType.NVarChar).Value = SomeValue;

Is it recommended that I set the size of the paramter like this:
cmd.Parameters.Add("@SomeParamter", SqlDbType.NVarChar, 100).Value = SomeValue;
 
or it's the same? And also, if the paramter is NText how would I set the size for it?

Thanks in advance
0
Comment
Question by:JessyEzzy
  • 2
  • 2
4 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20372621
if you do not set the size explicitely, it will be "set" implicitely by the value you assign to the parameter.
0
 
LVL 3

Author Comment

by:JessyEzzy
ID: 20384108
Set implicitly at which stage? And does setting or not setting the size affects performance?

Thanks
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 total points
ID: 20385397
>Set implicitly at which stage?

at "this" stage:
.Value = SomeValue;

which is "after" this part:
cmd.Parameters.Add("@SomeParamter", SqlDbType.NVarChar)

>And does setting or not setting the size affects performance?
no. but it does "increase" the readability, if you consistently use the same size there as in the procedure, as that way, you could see more easily when the value you try to pass is larger than what the procedure will "accept".
0
 
LVL 3

Author Closing Comment

by:JessyEzzy
ID: 31411630
Thanks
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question