What is best way to catch SQL Server errors in C#?

  • Thread starter Thread starter SagaV9
  • Start date Start date
S

SagaV9

Guest
Hi all, I am working with Windows 7/10, VS 2015 and SQL Server 2008R2.

I am currently working on a C# Windows Forms App that interacts with SQL Server. I have a class that handles the call to the server and works with connections, sqlcommands and dataadapters. The upper level code instantiates this class and calls the method to run my TSQL scripts.

I am catching SQLExceptions Time out (#-2) and Server not found (#-1) successfully, but want to ask where is the best place to do this.

If I put the catch in the class method I still need to throw an exception so that the calling code will handle it accordingly.

If I put the code in the calling code, I will have to repeat it everywhere where I call the class method.

Best practice says to catch the error in the calling code, but I'd like to ask here to see how it is typically done. As always, thank you for your time. Saga



You can't take the sky from me

Continue reading...
 
Back
Top