Dapper. What Exceptions does conn.Execute throw?
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.
![Jack Pettinger](https://i.stack.imgur.com/RDNFE.jpg?s=256&g=1)
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, 2022Comments
-
Jack Pettinger about 2 years
I'm using dapper.net and I've wrapped
connection.Execute
for myDelete
andUpdate
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 assumeSqlException
, 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 almost 9 yearsIf 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 almost 9 yearsDapper doesn't mask any exceptions. If your database provider is SQL, it will throw a
SqlException
.
-
-
Andre about 3 yearsNice, but can I use DbException instead? I do not want to bring System.Data.SqlClient package to the code
-
JDTLH9 about 3 yearsSqlException is derived from DbException so you should be able to catch on just DbException.