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? | | | | re: Why is it neccesary to include SqlDbType to the SqlParameter?
> It also seem annoying that every time I make a change in the database[color=blue]
> (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?[/color]
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.agerskov@get2net.dk> wrote in message
news:13664bff.0501050853.ff03210@posting.google.co m...[color=blue]
> 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?[/color] | | | | re: Why is it neccesary to include SqlDbType to the SqlParameter?
Replying to myself. :)
[color=blue]
> 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?[/color]
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:OWcTgo28EHA.3908@TK2MSFTNGP12.phx.gbl...[color=blue][color=green]
>> 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?[/color]
>
> 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.agerskov@get2net.dk> wrote in message
> news:13664bff.0501050853.ff03210@posting.google.co m...[color=green]
>> 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?[/color]
>
>[/color] | | | | re: Why is it neccesary to include SqlDbType to the SqlParameter?
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:OjgrJs28EHA.3012@TK2MSFTNGP09.phx.gbl...[color=blue]
> Replying to myself. :)
>[color=green]
> > 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?[/color]
>
> 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:OWcTgo28EHA.3908@TK2MSFTNGP12.phx.gbl...[color=green][color=darkred]
> >> 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?[/color]
> >
> > CommandBuilder has a DeriveParameters method (akin to the old ADO[/color][/color]
refresh[color=blue][color=green]
> > method), but it is not recommended and will create an extra round trip[/color][/color]
to[color=blue][color=green]
> > 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.agerskov@get2net.dk> wrote in message
> > news:13664bff.0501050853.ff03210@posting.google.co m...[color=darkred]
> >> 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?[/color]
> >
> >[/color]
>
>[/color] | | | | re: Why is it neccesary to include SqlDbType to the SqlParameter?
> Try to not specify the size and insert a long string in the DB. Is it[color=blue]
> truncated ?[/color]
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" <nobody@nowhere.com> wrote in message
news:eXA5lQC9EHA.3416@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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:OjgrJs28EHA.3012@TK2MSFTNGP09.phx.gbl...[color=green]
>> Replying to myself. :)
>>[color=darkred]
>> > 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?[/color]
>>
>> 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:OWcTgo28EHA.3908@TK2MSFTNGP12.phx.gbl...[color=darkred]
>> >> 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[/color][/color]
> refresh[color=green][color=darkred]
>> > method), but it is not recommended and will create an extra round trip[/color][/color]
> to[color=green][color=darkred]
>> > 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.agerskov@get2net.dk> wrote in message
>> > news:13664bff.0501050853.ff03210@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?
>> >
>> >[/color]
>>
>>[/color]
>
>[/color] | | | | re: Why is it neccesary to include SqlDbType to the SqlParameter?
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:OXsDJ8M9EHA.3588@TK2MSFTNGP10.phx.gbl...[color=blue][color=green]
> > Try to not specify the size and insert a long string in the DB. Is it
> > truncated ?[/color]
>
> 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[/color]
the[color=blue]
> 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 =[/color]
New[color=blue]
> 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" <nobody@nowhere.com> wrote in message
> news:eXA5lQC9EHA.3416@TK2MSFTNGP09.phx.gbl...[color=green]
> > 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[/color][/color]
message[color=blue][color=green]
> > de news:OjgrJs28EHA.3012@TK2MSFTNGP09.phx.gbl...[color=darkred]
> >> 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[/color][/color][/color]
not[color=blue][color=green][color=darkred]
> >> 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:OWcTgo28EHA.3908@TK2MSFTNGP12.phx.gbl...
> >> >> It also seem annoying that every time I make a change in the[/color][/color][/color]
database[color=blue][color=green][color=darkred]
> >> >> (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[/color]
> > refresh[color=darkred]
> >> > method), but it is not recommended and will create an extra round[/color][/color][/color]
trip[color=blue][color=green]
> > to[color=darkred]
> >> > 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,[/color][/color][/color]
and[color=blue][color=green][color=darkred]
> >> > 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[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> >> > 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.agerskov@get2net.dk> wrote in message
> >> > news:13664bff.0501050853.ff03210@posting.google.co m...
> >> >> Can anyone explain me why it is neccesary to include SqlDbType to[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> >> >> SqlParameter. In every example I see, it is done, but no one[/color][/color][/color]
explaines[color=blue][color=green][color=darkred]
> >> >> 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[/color][/color][/color]
same[color=blue][color=green][color=darkred]
> >> >> (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[/color][/color][/color]
database[color=blue][color=green][color=darkred]
> >> >> (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?
> >> >
> >> >
> >>
> >>[/color]
> >
> >[/color]
>
>[/color] |  | Similar .NET Framework bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|