I had a heated discussion with a colleague on the usage of stored procedures (SP) in a .NET
application (on an SQL server 2005 database). [He has a Microsoft background and I Java - which may or may not be relevant].
I have to insert data captured in the UI. For this I would write a SP and use that in the .NET
code? It's not required but what are the pros and cons of using a SP?
Another scenario:
I am maintaining a list of cities. User can add cities using the UI. As you expect user cannot enter a duplicate city. An error will be shown if duplicate entry happens. this can be implemented in a number of ways:
- In my code run a select query to check if it already exists and then if not insert the city otherwise an error on UI.
- Insert directly and due to unique index an
SQLException
will be caught. Introspect the SQLException to check what unique index is violated and show the respective error. - create one SP and in that handle the above logic, i.e. checking for duplicate and throw error or insert
Which one is the right way? (links to good resources are welcome).