Define sql exception in C #?

how to catch sql exception with error code (no error) to determine which exception is precisely selected in C #? for example, the database is offline or there is a reference row in another table, so sql will not allow me to delete the row, but how exactly can I determine what the problem is in catch so that I can display a message to the user in my application

+3
source share
2 answers

You need to put the database code in a block try ... catchand (assuming you are using ADO.NET for SQL Server), then drag and drop SqlException.

try
{
   // your database code here
}
catch(SqlException sqlEx)
{ 
   foreach(SqlError error in sqlEx.Errors)
   {
       // you can inspect the individual errors, their code etc. here
   }
}

SqlException SqlError, , - , .

- !

+8

.net ,

+1

All Articles