473,466 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Why is it neccesary to include SqlDbType to the SqlParameter?

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 Sql Server database
through a stored procedure-call. In the database it is defined as a
SmallDateTime. Every 3 methods in the client-code below gives the same
(and correct) result. So why is it that important?

dim param As SqlParameter

1. param = New SqlParameter("@DOB", SqlDbType.SmallDateTime)
param.Value = textboxDOB

2. param = New SqlParameter("@DOB", SqlDbType.DateTime)
param.Value = textboxDOB

3. param = New SqlParameter()
param.ParameterName = "@DOB"
param.Value = textboxDOB

It also seem annoying that every time I make a change in the database
(eg varchar(50) to varchar(100)), I have to check all my
Sqlparameters. Is there a way to read these definitions from the
stored procedure into the Sqlparameters?
Jul 21 '05 #1
5 3094
> It also seem annoying that every time I make a change in the database
(eg varchar(50) to varchar(100)), I have to check all my
Sqlparameters. Is there a way to read these definitions from the
stored procedure into the Sqlparameters?
CommandBuilder has a DeriveParameters method (akin to the old ADO refresh
method), but it is not recommended and will create an extra round trip to
the database.

One of the overloads for creating a parameter is just the name and type,
ommitting the size. I am not aware of any negative side affects of this for
varchar (etc.) parameters. Anybody know?

As for me, I have the same problem. I've been including the size, and then
when it changes in the sproc, I gotta go change it in the code too. ;(

As for your example; if you pass your DOB variable using the
SqlDbType.SmallDateTime you're not going to see a difference unless the
precision of your DOB variable is to the second! Check out the SQL datetime
and smalldatetime datatypes in BOL for more info.

Greg

"Kenneth" <k.********@get2net.dk> wrote in message
news:13*************************@posting.google.co m... 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 Sql Server database
through a stored procedure-call. In the database it is defined as a
SmallDateTime. Every 3 methods in the client-code below gives the same
(and correct) result. So why is it that important?

dim param As SqlParameter

1. param = New SqlParameter("@DOB", SqlDbType.SmallDateTime)
param.Value = textboxDOB

2. param = New SqlParameter("@DOB", SqlDbType.DateTime)
param.Value = textboxDOB

3. param = New SqlParameter()
param.ParameterName = "@DOB"
param.Value = textboxDOB

It also seem annoying that every time I make a change in the database
(eg varchar(50) to varchar(100)), I have to check all my
Sqlparameters. Is there a way to read these definitions from the
stored procedure into the Sqlparameters?

Jul 21 '05 #2
Replying to myself. :)
One of the overloads for creating a parameter is just the name and type,
ommitting the size. I am not aware of any negative side affects of this
for varchar (etc.) parameters. Anybody know?


Just saw this in documentation:

"The Size is inferred from the value of the dbType parameter if it is not
explicitly set in the size parameter."

So what does this mean for a varchar?

Greg
"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:OW**************@TK2MSFTNGP12.phx.gbl...
It also seem annoying that every time I make a change in the database
(eg varchar(50) to varchar(100)), I have to check all my
Sqlparameters. Is there a way to read these definitions from the
stored procedure into the Sqlparameters?


CommandBuilder has a DeriveParameters method (akin to the old ADO refresh
method), but it is not recommended and will create an extra round trip to
the database.

One of the overloads for creating a parameter is just the name and type,
ommitting the size. I am not aware of any negative side affects of this
for varchar (etc.) parameters. Anybody know?

As for me, I have the same problem. I've been including the size, and
then when it changes in the sproc, I gotta go change it in the code too.
;(

As for your example; if you pass your DOB variable using the
SqlDbType.SmallDateTime you're not going to see a difference unless the
precision of your DOB variable is to the second! Check out the SQL
datetime and smalldatetime datatypes in BOL for more info.

Greg

"Kenneth" <k.********@get2net.dk> wrote in message
news:13*************************@posting.google.co m...
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 Sql Server database
through a stored procedure-call. In the database it is defined as a
SmallDateTime. Every 3 methods in the client-code below gives the same
(and correct) result. So why is it that important?

dim param As SqlParameter

1. param = New SqlParameter("@DOB", SqlDbType.SmallDateTime)
param.Value = textboxDOB

2. param = New SqlParameter("@DOB", SqlDbType.DateTime)
param.Value = textboxDOB

3. param = New SqlParameter()
param.ParameterName = "@DOB"
param.Value = textboxDOB

It also seem annoying that every time I make a change in the database
(eg varchar(50) to varchar(100)), I have to check all my
Sqlparameters. Is there a way to read these definitions from the
stored procedure into the Sqlparameters?


Jul 21 '05 #3
Try to not specify the size and insert a long string in the DB. Is it
truncated ?

AFAIK it is valid to not specify a size for varchar when using Transact-SQL.
In this case the default length is 30. It makes me think that the .NET
provider could do something similar...

Thanks for letting us know what you find...

Patrice

--

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> a écrit dans le message
de news:Oj**************@TK2MSFTNGP09.phx.gbl...
Replying to myself. :)
One of the overloads for creating a parameter is just the name and type,
ommitting the size. I am not aware of any negative side affects of this
for varchar (etc.) parameters. Anybody know?


Just saw this in documentation:

"The Size is inferred from the value of the dbType parameter if it is not
explicitly set in the size parameter."

So what does this mean for a varchar?

Greg
"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:OW**************@TK2MSFTNGP12.phx.gbl...
It also seem annoying that every time I make a change in the database
(eg varchar(50) to varchar(100)), I have to check all my
Sqlparameters. Is there a way to read these definitions from the
stored procedure into the Sqlparameters?


CommandBuilder has a DeriveParameters method (akin to the old ADO refresh method), but it is not recommended and will create an extra round trip to the database.

One of the overloads for creating a parameter is just the name and type,
ommitting the size. I am not aware of any negative side affects of this
for varchar (etc.) parameters. Anybody know?

As for me, I have the same problem. I've been including the size, and
then when it changes in the sproc, I gotta go change it in the code too.
;(

As for your example; if you pass your DOB variable using the
SqlDbType.SmallDateTime you're not going to see a difference unless the
precision of your DOB variable is to the second! Check out the SQL
datetime and smalldatetime datatypes in BOL for more info.

Greg

"Kenneth" <k.********@get2net.dk> wrote in message
news:13*************************@posting.google.co m...
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 Sql Server database
through a stored procedure-call. In the database it is defined as a
SmallDateTime. Every 3 methods in the client-code below gives the same
(and correct) result. So why is it that important?

dim param As SqlParameter

1. param = New SqlParameter("@DOB", SqlDbType.SmallDateTime)
param.Value = textboxDOB

2. param = New SqlParameter("@DOB", SqlDbType.DateTime)
param.Value = textboxDOB

3. param = New SqlParameter()
param.ParameterName = "@DOB"
param.Value = textboxDOB

It also seem annoying that every time I make a change in the database
(eg varchar(50) to varchar(100)), I have to check all my
Sqlparameters. Is there a way to read these definitions from the
stored procedure into the Sqlparameters?



Jul 21 '05 #4
> Try to not specify the size and insert a long string in the DB. Is it
truncated ?
No.

I you try and insert a string longer than the field length without
specifying the field length in the parameter it throws an exception!
"System.Data.SqlClient.SqlException: String or binary data would be
truncated."

It does NOT throw the exception when you do specify the field length in the
parameter, it simpy truncates.

Here is the code I tested with:

Dim cn As New SqlConnection("data source=.;initial
catalog=northwind;integrated security=SSPI;persist security
info=False;packet size=4096;")

Dim cmd As New SqlCommand("INSERT shippers (CompanyName, Phone)
VALUES (@company_name, @phone)", cn)

cmd.Parameters.Add("@company_name", SqlDbType.NVarChar).Value = New
String("X"c, 41) ' allows 40
cmd.Parameters.Add("@phone", SqlDbType.NVarChar).Value = New
String("Z"c, 25) ' allows 24

Try
cn.Open()
cmd.ExecuteNonQuery()
Catch ex As Exception
Debug.WriteLine(ex.ToString)
Finally
If Not cn Is Nothing AndAlso cn.State = ConnectionState.Open
Then cn.Close()
End Try
Out of curiosity I tried with a sproc also with the same results:

CREATE PROCEDURE usp_InsertShipper
@company_name nvarchar(40),
@phone nvarchar(25)

AS

SET NOCOUNT ON

INSERT shippers (CompanyName, Phone)
VALUES (@company_name, @phone)

Greg
"Patrice" <no****@nowhere.com> wrote in message
news:eX**************@TK2MSFTNGP09.phx.gbl... Try to not specify the size and insert a long string in the DB. Is it
truncated ?

AFAIK it is valid to not specify a size for varchar when using
Transact-SQL.
In this case the default length is 30. It makes me think that the .NET
provider could do something similar...

Thanks for letting us know what you find...

Patrice

--

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> a écrit dans le message
de news:Oj**************@TK2MSFTNGP09.phx.gbl...
Replying to myself. :)
> One of the overloads for creating a parameter is just the name and
> type,
> ommitting the size. I am not aware of any negative side affects of
> this
> for varchar (etc.) parameters. Anybody know?


Just saw this in documentation:

"The Size is inferred from the value of the dbType parameter if it is not
explicitly set in the size parameter."

So what does this mean for a varchar?

Greg
"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:OW**************@TK2MSFTNGP12.phx.gbl...
>> It also seem annoying that every time I make a change in the database
>> (eg varchar(50) to varchar(100)), I have to check all my
>> Sqlparameters. Is there a way to read these definitions from the
>> stored procedure into the Sqlparameters?
>
> CommandBuilder has a DeriveParameters method (akin to the old ADO refresh > method), but it is not recommended and will create an extra round trip to > the database.
>
> One of the overloads for creating a parameter is just the name and
> type,
> ommitting the size. I am not aware of any negative side affects of
> this
> for varchar (etc.) parameters. Anybody know?
>
> As for me, I have the same problem. I've been including the size, and
> then when it changes in the sproc, I gotta go change it in the code
> too.
> ;(
>
> As for your example; if you pass your DOB variable using the
> SqlDbType.SmallDateTime you're not going to see a difference unless the
> precision of your DOB variable is to the second! Check out the SQL
> datetime and smalldatetime datatypes in BOL for more info.
>
> Greg
>
> "Kenneth" <k.********@get2net.dk> wrote in message
> news:13*************************@posting.google.co m...
>> 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 Sql Server database
>> through a stored procedure-call. In the database it is defined as a
>> SmallDateTime. Every 3 methods in the client-code below gives the same
>> (and correct) result. So why is it that important?
>>
>> dim param As SqlParameter
>>
>> 1. param = New SqlParameter("@DOB", SqlDbType.SmallDateTime)
>> param.Value = textboxDOB
>>
>> 2. param = New SqlParameter("@DOB", SqlDbType.DateTime)
>> param.Value = textboxDOB
>>
>> 3. param = New SqlParameter()
>> param.ParameterName = "@DOB"
>> param.Value = textboxDOB
>>
>> It also seem annoying that every time I make a change in the database
>> (eg varchar(50) to varchar(100)), I have to check all my
>> Sqlparameters. Is there a way to read these definitions from the
>> stored procedure into the Sqlparameters?
>
>



Jul 21 '05 #5
Thanks, interesting.

The length is used client side to truncate the string. If the length is not
known the string is sent entirely to the server that will eventuallty raise
the usual error when data doesn't fit the alloted space...

Patrice

--

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> a écrit dans le message
de news:OX**************@TK2MSFTNGP10.phx.gbl...
Try to not specify the size and insert a long string in the DB. Is it
truncated ?
No.

I you try and insert a string longer than the field length without
specifying the field length in the parameter it throws an exception!
"System.Data.SqlClient.SqlException: String or binary data would be
truncated."

It does NOT throw the exception when you do specify the field length in

the parameter, it simpy truncates.

Here is the code I tested with:

Dim cn As New SqlConnection("data source=.;initial
catalog=northwind;integrated security=SSPI;persist security
info=False;packet size=4096;")

Dim cmd As New SqlCommand("INSERT shippers (CompanyName, Phone)
VALUES (@company_name, @phone)", cn)

cmd.Parameters.Add("@company_name", SqlDbType.NVarChar).Value = New String("X"c, 41) ' allows 40
cmd.Parameters.Add("@phone", SqlDbType.NVarChar).Value = New
String("Z"c, 25) ' allows 24

Try
cn.Open()
cmd.ExecuteNonQuery()
Catch ex As Exception
Debug.WriteLine(ex.ToString)
Finally
If Not cn Is Nothing AndAlso cn.State = ConnectionState.Open
Then cn.Close()
End Try
Out of curiosity I tried with a sproc also with the same results:

CREATE PROCEDURE usp_InsertShipper
@company_name nvarchar(40),
@phone nvarchar(25)

AS

SET NOCOUNT ON

INSERT shippers (CompanyName, Phone)
VALUES (@company_name, @phone)

Greg
"Patrice" <no****@nowhere.com> wrote in message
news:eX**************@TK2MSFTNGP09.phx.gbl...
Try to not specify the size and insert a long string in the DB. Is it
truncated ?

AFAIK it is valid to not specify a size for varchar when using
Transact-SQL.
In this case the default length is 30. It makes me think that the .NET
provider could do something similar...

Thanks for letting us know what you find...

Patrice

--

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> a écrit dans le message de news:Oj**************@TK2MSFTNGP09.phx.gbl...
Replying to myself. :)

> One of the overloads for creating a parameter is just the name and
> type,
> ommitting the size. I am not aware of any negative side affects of
> this
> for varchar (etc.) parameters. Anybody know?

Just saw this in documentation:

"The Size is inferred from the value of the dbType parameter if it is not explicitly set in the size parameter."

So what does this mean for a varchar?

Greg
"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:OW**************@TK2MSFTNGP12.phx.gbl...
>> It also seem annoying that every time I make a change in the database >> (eg varchar(50) to varchar(100)), I have to check all my
>> Sqlparameters. Is there a way to read these definitions from the
>> stored procedure into the Sqlparameters?
>
> CommandBuilder has a DeriveParameters method (akin to the old ADO

refresh
> method), but it is not recommended and will create an extra round trip
to
> the database.
>
> One of the overloads for creating a parameter is just the name and
> type,
> ommitting the size. I am not aware of any negative side affects of
> this
> for varchar (etc.) parameters. Anybody know?
>
> As for me, I have the same problem. I've been including the size,

and > then when it changes in the sproc, I gotta go change it in the code
> too.
> ;(
>
> As for your example; if you pass your DOB variable using the
> SqlDbType.SmallDateTime you're not going to see a difference unless the > precision of your DOB variable is to the second! Check out the SQL
> datetime and smalldatetime datatypes in BOL for more info.
>
> Greg
>
> "Kenneth" <k.********@get2net.dk> wrote in message
> news:13*************************@posting.google.co m...
>> 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 Sql Server database
>> through a stored procedure-call. In the database it is defined as a
>> SmallDateTime. Every 3 methods in the client-code below gives the same >> (and correct) result. So why is it that important?
>>
>> dim param As SqlParameter
>>
>> 1. param = New SqlParameter("@DOB", SqlDbType.SmallDateTime)
>> param.Value = textboxDOB
>>
>> 2. param = New SqlParameter("@DOB", SqlDbType.DateTime)
>> param.Value = textboxDOB
>>
>> 3. param = New SqlParameter()
>> param.ParameterName = "@DOB"
>> param.Value = textboxDOB
>>
>> It also seem annoying that every time I make a change in the database >> (eg varchar(50) to varchar(100)), I have to check all my
>> Sqlparameters. Is there a way to read these definitions from the
>> stored procedure into the Sqlparameters?
>
>



Jul 21 '05 #6

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

Similar topics

2
by: valmir cinquini | last post by:
Hi everybody I'm newbee in C# and I'm supporting an application where there's a method like following: public int addNews(DateTime dtNews, string strTitle, string strShortText, string...
11
by: Vinod I | last post by:
Hi Team, I am having a string as "System.Data.SqlDbType.Int". Now I want to convert this string type to actual type to use with my Command object Parameter Creation. How I will convert this...
3
by: Amil | last post by:
Hi all, When I call a stored procedure, one of the output parameter returns a sqldbtype.integer but when assigned to a C# int field/property I get this error: int myfield; // say, parms is the...
3
by: Narshe | last post by:
If I use this simple code SqlParameter param = new SqlParameter( "@param", SqlDbType.DateTime ); param.Value = System.DateTime.Now; param.Value is "11/2/2005" rather than "11/2/2005 10:42:15...
2
by: adams114 | last post by:
I am having a strange problem with invalid type casts. I am trying to update a MS SQL Database with a stored procedure. When I setup the parameters collection for the command object I get a invalid...
10
by: Patrick Olurotimi Ige | last post by:
I have a checkbox and i want to input Char "Y" or "N" to the Table In C# we could use for example :- ptrTest.Value = chkYN.Checked ? "Y" : "N"; Whats the equivalent in VB.NET?
5
by: Kevin R | last post by:
I'm trying to update a sql database. It's modified Oledb code from an example that did work with an access database. How can I tweak my code to make it work? Thanks in advance. Kevin...
5
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...
4
by: Dabbler | last post by:
Is there a table of sizes to use for SqlDbTypes with SqlParameter constructor such as: SqlParameter("@VanId", SqlDbType.Int, 4")); Not sure which types require a size and which ones can be left...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.