acl wrote:
Thanks for the explanation. This finally makes sense.
(As a side-note regarding the modified query from my previous post: I indeed forgot to change the parameter type; I corrected this now.)
I still don't get your bewilderment at my comparing a string to a string of different size. As I explained: if you were doing this in pure SQL, you wouldn't even think about it.
With parameters in sql you mean? You'd get the same thing. Or an error perhaps, haven't checked.
Comparing a varchar(3) field with a string longer than 3 will never result in a row, it will always result in 0 rows. I understand that by the truncation of the value, it's resulting in some rows perhaps as the fragment matches, but it's more of an 'unspecified' behavior: your comparison otherwise would never match any row. I'm not bewildered, I just wondered why one would compare a string much longer than any value in the table column
Also: if I'd do this with a prepared statement in ADO.NET, I would use Parameters.AddWithValue and don't worry about the string length.
But ok, I understand now that you stick to the exact parameter type. Honestly, I would prefer an exception to a truncated string in this case.
AddWithValue will likely set the parameter to the length of the value. That's a bit of a problem tho
-> first you'll get a comparison mismatch, which the DB likely will not sweat about (but you will if lengths cross a certain length), but more importantly, every query will be different and it can't likely re-use cached execution plans (as the parameter sizes differ). so the alternative is to send the parameter with length 4000. I think EF did that in its first year and it wasn't a success.
The option for an exception is fair, though I think you'd be posting the same thread here if you'd have gotten an exception
"WHy does this throw an exception?", which is a fair question in that context, as you assumed the value would be passed as-is.
It's a leaky abstraction, as you'd be required to know the length of the field in the DB. I'll add a workitem to make it configurable to get an exception in this case (when a parameter value is truncated).