Dapper. What Exceptions does conn.Execute throw?

10,321

Dapper will indeed throw SqlException when used against a SqlConnection.

The exceptions will be consistent with those that you will get from vanilla ADO.NET code. With a value that is the code of the specific SQL exception type.

Share:
10,321
Jack Pettinger
Author by

Jack Pettinger

I've been using Winform/ASP.NET since 2009 working with: vb.net c# html css javascript jquery mssql-2000 mssql-2005 mssql-2008 and more recently: android SOreadytohelp

Updated on June 08, 2022

Comments

  • Jack Pettinger
    Jack Pettinger about 2 years

    I'm using dapper.net and I've wrapped connection.Execute for my Delete and Update statements.

    public virtual void Update(TEntity entity)
    {
        IDbConnection connection = connectionService.Connection;
    
        connection.Execute(UpdateQuery, entity, connectionService.Transaction);
    }
    

    When I've come to use this, I want to know if it fails or not by catching an exception(s) and not just a general Exception. Which (if any) exceptions are thrown? I would assume SqlException, but a previous manager taught me to assume nothing.

    Can anybody point me in the right direction as to where I find this information?

    • JDTLH9
      JDTLH9 almost 9 years
      If connectionService.Connection returns a SqlConnection then my answer below is correct. I have search for some online documentation but it is scarce (hence why you ended up here). I know this from practice as I have been using dapper as you want to just this week :)
    • Yuval Itzchakov
      Yuval Itzchakov almost 9 years
      Dapper doesn't mask any exceptions. If your database provider is SQL, it will throw a SqlException.
  • Andre
    Andre about 3 years
    Nice, but can I use DbException instead? I do not want to bring System.Data.SqlClient package to the code
  • JDTLH9
    JDTLH9 about 3 years
    SqlException is derived from DbException so you should be able to catch on just DbException.