Re: nvarchar parameter not truncated based on size of field
- From: rh <rh@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 22 Feb 2007 13:10:04 -0800
Bill, Thanks for the info. I feel like a celebrity just answered my question!
It's still kind of bothering me that this functionality has changed since
..NetCF 1.0 (caught me off guard and almost went into production because I had
no reason to believe the SqlCeParameter object would work differently than
before), so I did some testing on other versions of the framework and this is
what I found:
..NETCF 1.x - Truncates parameter automatically
..NETCF 2.0 - Raises error from engine
..NET 1.1 (full framework) - Truncates parameter automatically
..NET 2.0 (full framework) - Truncates parameter automatically
..NET 2.0 (full framework - SqlCe3.1 client library) - Raises error from engine
So this behavior seems to only be in the SqlCe3.x library - would there be
any explanation for why this library behaves differently?
"William (Bill) Vaughn" wrote:
While I know Ginny is right, I find it tiresome as well. Ah, yes. I.
recommend that all values passed as parameters be pre-validated. This means
more than matching the datatype. It means range-checking, validation against
business rules and verifying the length of strings. It's a lot easier to
perform these functions before the engine chokes on the parameter or value
passed.
hth
--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------
"rh" <rh@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:2C899377-4CF6-47F6-B1FB-25B2B8AB3E35@xxxxxxxxxxxxxxxx
I guess I've gotten used to the old (lazy) way and prefer that. Anyway, so
what's the best practice when assigning strings to parameters? Does
everyone
check the size of each string before assigning? I've never seen an example
done that way. Or do people just let the error happen and report back to
the
user? Or something else better?
Thanks for the response Ginny.
"Ginny Caughey [MVP]" wrote:
My opinion is that the current behavior is correct.
FWIW,
--
Ginny
"rh" <rh@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:7973F761-2780-41FF-B761-CF2640FF0AFB@xxxxxxxxxxxxxxxx
We just migrated an app from .NETCF 1.0 to .NETCF 2.0 and SQL Server
2005
Compact Edition (3.1?) and I'm running into a problem where the
nvarchar
parameters aren't truncated based on the size of the field. For
example:
Dim cmd As SqlCeCommand = New SqlCeCommand(sSql)
cmd.Parameters.Add("@p1", SqlDbType.NVarChar, 25, "Note").Value = sVal
If the length of sVal is 26, I get an error like:
String truncation: max=25, len=26, value=whatever the value was...
In previous versions of .NETCF/SqlCe, this didn't happen. The string
would
just automatically get truncated and the truncated string would
successfully
be inserted into the database.
I guess I can see arguments both ways for this but I'm trying to find
out
what the expected bahavior is here so I can properly assign the
parameters
in
my queries without throwing exceptions or find out if there is some new
property on the command or parameter objects to automatically truncated
values when they are too large.
Thanks.
- Follow-Ups:
- Re: nvarchar parameter not truncated based on size of field
- From: William \(Bill\) Vaughn
- Re: nvarchar parameter not truncated based on size of field
- References:
- Re: nvarchar parameter not truncated based on size of field
- From: Ginny Caughey [MVP]
- Re: nvarchar parameter not truncated based on size of field
- From: William \(Bill\) Vaughn
- Re: nvarchar parameter not truncated based on size of field
- Prev by Date: Re: nvarchar parameter not truncated based on size of field
- Next by Date: Re: nvarchar parameter not truncated based on size of field
- Previous by thread: Re: nvarchar parameter not truncated based on size of field
- Next by thread: Re: nvarchar parameter not truncated based on size of field
- Index(es):
Relevant Pages
|