【问题标题】:How to use SqlException in .NET CORE?如何在 .NET CORE 中使用 SqlException?
【发布时间】:2018-11-24 09:33:50
【问题描述】:

在我使用 .Net Framework 的旧项目中使用

 using System.Data.SqlClient;

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

现在在 .NET CORE 中不知道上面的代码

如何在.NET CORE 中使用typeof(SqlException)

【问题讨论】:

  • 这将在 .NET Core 中编译得很好。您可能没有将System.Data.SqlClient 包添加到您的项目中。否则,请提供您得到的确切错误消息,以及实际编译它的其余代码,而不是 sn-p。

标签: exception asp.net-core exception-handling entity-framework-core ef-core-2.1


【解决方案1】:

在.Net Core版本中,使用前需要安装这个包:Microsoft.EntityFrameworkCore.SqlServer

安装包 Microsoft.EntityFrameworkCore.SqlServer -Version 2.1.4

try
{
    // code goes here...    
}
catch (SqlException sqlEx)
{
    // code goes here...
}
catch (Exception ex)
{
    // other exception...
}

【讨论】:

  • 要在同一块中捕获SqlExceptionSqliteException,可以使用DbException,因为它是SqlSqlite 的基础 供应商,毫无疑问是未来的供应商。
猜你喜欢
  • 2021-08-28
  • 2016-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-21
  • 2017-05-01
  • 1970-01-01
相关资源
最近更新 更多