By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,397 Members | 2,552 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,397 IT Pros & Developers. It's quick & easy.

Why is it neccesary to include SqlDbType to the SqlParameter?

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
> 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

P: n/a
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

P: n/a
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

P: n/a
> 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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.