Solved

Setting the paramter size for a SqlParameter

Posted on 2007-11-29
4
218 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]
Comment Utility
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
Comment Utility
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
Comment Utility
>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
Comment Utility
Thanks
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now