Solved

Setting the paramter size for a SqlParameter

Posted on 2007-11-29
4
221 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 143

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 143

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 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