Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SqlException in .NET CORE?

In my old project with .Net Framework use from

 using System.Data.SqlClient;

and

if (ex.GetType() == typeof(SqlException))
{
    return ErrorMessage((SqlException)ex);
}

and now in .NET CORE Does not know Code above

How to use typeof(SqlException) in .NET CORE?

like image 944
Alireza Avatar asked Nov 24 '18 09:11

Alireza


People also ask

What is SQLException number?

This is a wrapper for the Number property of the first SqlError in the Errors property. If Errors is null , the default value for int is returned. For more information on SQL Server engine errors, see Database Engine Events and Errors.

What is the class property in SQLException?

Property ValueA value from 1 to 25 that indicates the severity level of the error.

What is System data SqlClient SQLException?

The System. Data. SqlClient. SqlException is typically thrown when an accessed SQL Server returns a warning or error of its own.


1 Answers

In .Net Core version, you need to install this package before using it: Microsoft.EntityFrameworkCore.SqlServer

Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 2.1.4

try
{
    // code goes here...    
}
catch (SqlException sqlEx)
{
    // code goes here...
}
catch (Exception ex)
{
    // other exception...
}
like image 199
Tân Avatar answered Oct 03 '22 22:10

Tân