Re: Why aren't data types in SqlParameters validated?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Mon, 26 Feb 2007 02:17:32 -0500, Jerad Rose wrote:

I believe this question is simple enough -- why aren't data types in
SqlParameters validated in ADO.NET?

For example, why is an exception *not* thrown on this line:

cmd.Parameters.Add(New SqlParameter("@Id", Data.SqlDbType.Int)).Value = "not
an int"

Ideally, this wouldn't even compile, let alone execute. But it's even worse
that this line will execute, only later to bomb out when the stored proc is
executed, giving me this error:

Failed to convert parameter value from a String to a Int32.

Which can lead you on a wild goose chase tracking down the one out of
possibly several parameters that had a type issue.

My next question is, if the data type parameter in this constructor is not
for the sake of type safety, what is its purpose?

Lastly, is there anything that can be done (within reason) to ensure type
safety when setting parameter values, so that an exeption is thrown on the
line where the parameter value was attempted to be set? If not, what is the
best practice for accomplishing type safety when setting up store procedures
in ADO.NET?

Just FYI, this is in reference to ADO.NET 2.0.

Thanks in advance for your feedback.

Jerad

Probably because stored procedures have the same behavior and will try to
convert data types during execution ...

Also, it would be difficult to enforce if some of the parameters being
passed is a user defined data type

--
Bits.Bytes
http://bytes.thinkersroom.com
.



Relevant Pages

  • Re: Why arent data types in SqlParameters validated?
    ... Ideally, this wouldn't even compile, let alone execute. ... Lastly, is there anything that can be done to ensure type safety when setting parameter values, so that an exeption is thrown on the line where the parameter value was attempted to be set? ...
    (microsoft.public.dotnet.framework.adonet)
  • Why arent data types in SqlParameters validated?
    ... I believe this question is simple enough -- why aren't data types in ... why is an exception *not* thrown on this line: ... Ideally, this wouldn't even compile, let alone execute. ... best practice for accomplishing type safety when setting up store procedures ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Whats finally keyword good for?
    ... You only delete the file if IOException is thrown? ... Then what about the exceptions that could be thrown from writing to the ... Wouldn't fooexecute no matter what? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Determining whether an applications has thrown some error ...
    ... set some flag and check the flag whether error is thrown or not ... ... It depends on where the error happens and what type it is versus where you want to execute the code. ... If the error is an exception (as your use of the word "thrown" implies) you can put the code in a catch block and it will execute "immediately" after the error is thrown (depending on the rest of the code). ...
    (microsoft.public.dotnet.languages.csharp)
  • Cant get simple Swing app to come up on machine in Japan
    ... I have a simple application (just shows a JFrame with size set to ... 400,400) that I am trying to execute. ... exceptions being thrown. ...
    (comp.lang.java.gui)