Sql Exceptions and ADO.Net

Something I found the other day that confused me for a while are sql exceptions and ADO.Net. I had a test that I was expecting to throw an exception but “wouldn’t” – It would in query analyzer but if I got called Execute on the SqlCommand it would appear to go through fine – the data reader even had some rows in it – what was going on…

I would have expected the SqlCommand to throw the exception instantly – its level was high enough for it to fail! Well I found that this exception only bubbles out once you actually read the data down to the point where the exception occurred. I thought this was pretty strange since if you got a result set back then the first 3 reads from the data reader might be fine while the fourth one actually shows that an exception occurred. If you were doing a read followed by some processing counting on the execute having returned a complete and successful set then it could cause you some issues… it appears as though you should go through and get the results out / put them in a dataset or something first just to make sure that they are actually fine (?) – obviously its like that for a reason but it’s definitelly something to watch out for…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: