473,480 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SQLParameter Question.

There are sever overloads and ways of adding to the SqlCommand's
Parameters collection. They all specify the parameters name. But some
also supply a SqlDbType and a length. Does anyone know the pros/cons
of supplying or not supplying this info?

Oct 3 '07 #1
3 1607
On Oct 3, 3:38 pm, wackyph...@yahoo.com wrote:
There are sever overloads and ways of adding to the SqlCommand's
Parameters collection. They all specify the parameters name. But some
also supply a SqlDbType and a length. Does anyone know the pros/cons
of supplying or not supplying this info?
I've always specified both the SqlDbType and Length.

One advantage I can think of is you will avoid type errors.. for
instance say your Sql Column is DATETIME type, and you try to insert a
string "abc" into that column. If you specify the SqlDbType in
your .NET code, it will catch this error before sending the query to
the server (I think)

Another advantage for the length.. Say your Sql Column is
varchar(40).. and you don't specify the length of the varchar in
your .net. You could pass a string to the SqlParam's Value that is
longer than 40 characters, and the .NET will not care, but Sql Server
will raise a warning about the data being truncated.

Now if you DID specify the SqlParam's length to be 40, then .NET will
automatically truncate the string to 40 characters for you.. and no
error will be raised (but you may loose data and not know it).
I'm interested in other's responses.. it is pretty tedious to type
every Sql Type and Length when working with stored procs.

Oct 3 '07 #2
Kbalz wrote:
On Oct 3, 3:38 pm, wackyph...@yahoo.com wrote:
>There are sever overloads and ways of adding to the SqlCommand's
Parameters collection. They all specify the parameters name. But some
also supply a SqlDbType and a length. Does anyone know the pros/cons
of supplying or not supplying this info?

I've always specified both the SqlDbType and Length.
<...>
I'm interested in other's responses.. it is pretty tedious to type
every Sql Type and Length when working with stored procs.
I've never bothered but it has bitten me, twice.

Once when using the mysql driver and passing an initial value of null
(then populating it with the "real" value in a loop), it was not
inferring the correct type.

The second time was just recently with MSSQL 2005 SP1 and I had a very
interesting situation where I was retrieving data based on a varchar param.
When you dont specify the type as varchar, the command interpret's it as
nvarchar (makes sense since .net is unicode).
99.9% of the cases (all x 1) have worked fine, but in this case, sql
server would not match the where clause nvarchar param to the varchar
column values. It would randomly match 1 or 2 out of maybe 100.
Analysing the sql it uses sp_executesql and passes nvarchar params which
when run manually would fail the same as my program.
I tested against sp2 and it worked but it would in some cases work on
our test systems but not on prod.
Anyhoo, when I specified the type as varchar, it worked 100%.

I still dont bother specifying type or length.

:)

JB
Oct 4 '07 #3
Yeah, I've wondered if it may be faster to specify it. I don't know if
ADO.NET does reflection to find the type of object you pass it if you
don't explicitly tell it what it is or if it just can call toString()
on everything no matter what.

I wish MSDN was a bit more detailed about it, because I don't mind
doing it if it helps, but I often wonder if I'm doing more work for no
reason :)

Oct 5 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3097
by: Kenneth | last post by:
Can anyone explain me why it is neccesary to include SqlDbType to the SqlParameter. In every example I see, it is done, but no one explaines why. I have for example a date I want to save into my...
3
2431
by: jiangyh | last post by:
hi everyone: SqlParameter class has a property that is DbType looks like following source public DbType DbType { get { // TODO: Add DBParameter.DbType getter implementation return new...
5
4589
by: Daniel Groh | last post by:
What is the best way to access a procedure ? MyCommand.Parameters.Add("@param1",typeof(string)); or SqlParameter myParam = new SqlParameter(); myParam.Add("@param1",typeof(string)); What...
4
4322
by: Jason Huang | last post by:
Hi, I want to use the Sqlparameter and SqlDataAdapter to update my data, and the data will be updated based on two TextBoxes txtCustName and txtCustAddress. Thanks for help. Jason
5
8273
by: Jason Huang | last post by:
Hi, The SqlParameter myPM =new SqlParameter("@Address", txtAddress.Text) is working for update, but SqlParameter myPM =new SqlParameter ("@Address",SqlDbType.NVarChar,90,txtAddress.Text) is...
2
301
by: Stephan | last post by:
Hi, I'm experiencing the problem mentioned in the title above. Briefly, I build a page in which I include a class. This class contains a procedure to add a value to an ArrayList. The value is...
2
6270
by: hoz | last post by:
Hi , i have a complex ideas about sqlparameter lenght . lets say i have the following code mycom.Parameters.Add("@sip",SqlDbType.NVarChar); mycom.Parameters.Value = siparisid ; in the stored...
3
2738
by: Stacey Levine | last post by:
I have a webservice that has the below procedure. Basically a procedure to called a stored procedure and return the results. When I try to call the webservice from my program I get the error. Both...
1
2385
by: BobRoyAce | last post by:
I have a class that has several Subs that do DB things, some of which require the same set of parameters to be passed to a stored procedure. One class has 12 parameters and part of code used to...
6
7821
by: Tim Zych | last post by:
' Declare a new parameter object Dim param() As SqlParameter = New SqlParameter(0) {} ' Set this to null and make it an InputOutput parameter param(0) = New SqlParameter("@Something, DBNull.Value)...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7060
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6760
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4501
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3013
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.