473,606 Members | 2,381 Online
Bytes | Software Development & Data Engineering Community
+ 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.Small DateTime)
param.Value = textboxDOB

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

3. param = New SqlParameter()
param.Parameter Name = "@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 3109
> 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 DeriveParameter s 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.Small DateTime 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.********@get 2net.dk> wrote in message
news:13******** *************** **@posting.goog le.com... 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.Small DateTime)
param.Value = textboxDOB

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

3. param = New SqlParameter()
param.Parameter Name = "@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@DON T_SPAM_ME_hotma il.com> wrote in message
news:OW******** ******@TK2MSFTN GP12.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 DeriveParameter s 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.Small DateTime 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.********@get 2net.dk> wrote in message
news:13******** *************** **@posting.goog le.com...
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.Small DateTime)
param.Value = textboxDOB

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

3. param = New SqlParameter()
param.Parameter Name = "@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@DON T_SPAM_ME_hotma il.com> a écrit dans le message
de news:Oj******** ******@TK2MSFTN GP09.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@DON T_SPAM_ME_hotma il.com> wrote in message
news:OW******** ******@TK2MSFTN GP12.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 DeriveParameter s 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.Small DateTime 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.********@get 2net.dk> wrote in message
news:13******** *************** **@posting.goog le.com...
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.Small DateTime)
param.Value = textboxDOB

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

3. param = New SqlParameter()
param.Parameter Name = "@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.Sq lClient.SqlExce ption: 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=.;initia l
catalog=northwi nd;integrated security=SSPI;p ersist security
info=False;pack et size=4096;")

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

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

Try
cn.Open()
cmd.ExecuteNonQ uery()
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_InsertShipp er
@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******** ******@TK2MSFTN GP09.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@DON T_SPAM_ME_hotma il.com> a écrit dans le message
de news:Oj******** ******@TK2MSFTN GP09.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@DON T_SPAM_ME_hotma il.com> wrote in message
news:OW******** ******@TK2MSFTN GP12.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 DeriveParameter s 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.Small DateTime 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.********@get 2net.dk> wrote in message
> news:13******** *************** **@posting.goog le.com...
>> 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.Small DateTime)
>> param.Value = textboxDOB
>>
>> 2. param = New SqlParameter("@ DOB", SqlDbType.DateT ime)
>> param.Value = textboxDOB
>>
>> 3. param = New SqlParameter()
>> param.Parameter Name = "@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@DON T_SPAM_ME_hotma il.com> a écrit dans le message
de news:OX******** ******@TK2MSFTN GP10.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.Sq lClient.SqlExce ption: 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=.;initia l
catalog=northwi nd;integrated security=SSPI;p ersist security
info=False;pack et size=4096;")

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

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

Try
cn.Open()
cmd.ExecuteNonQ uery()
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_InsertShipp er
@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******** ******@TK2MSFTN GP09.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@DON T_SPAM_ME_hotma il.com> a écrit dans le message de news:Oj******** ******@TK2MSFTN GP09.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@DON T_SPAM_ME_hotma il.com> wrote in message
news:OW******** ******@TK2MSFTN GP12.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 DeriveParameter s 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.Small DateTime 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.********@get 2net.dk> wrote in message
> news:13******** *************** **@posting.goog le.com...
>> 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.Small DateTime)
>> param.Value = textboxDOB
>>
>> 2. param = New SqlParameter("@ DOB", SqlDbType.DateT ime)
>> param.Value = textboxDOB
>>
>> 3. param = New SqlParameter()
>> param.Parameter Name = "@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
12768
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 strText, int intStatus) When trying to add the data to a SqlServer 2K using ADO.Net I did the following:
11
4223
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 string to the type object ?. Thanks in advance
3
14450
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 sql integer data myfield = parms; // creates the error. Error 1 Cannot implicitly convert type 'System.Data.SqlClient.SqlParameter' to 'int'
3
13057
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 AM" How do I get it so the sql value is the same as the C# value, and not truncated?
2
2231
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 cast exception error: Compiler Error Message: BC30311: Value of type 'Date' cannot be converted to 'Integer'. The real problem here is that the type in the database and the stored prcedure aren't integers at all rather they are datetime...
10
3974
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
3199
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 ====== Code error:
5
389
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 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
4
2617
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 blank. Thanks
0
7955
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8440
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8431
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8096
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8306
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5966
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3937
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2448
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 we have to send another system
1
1557
muto222
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.