Azavea.Open.DAO.SQL.SqlConnectionUtilities.XSafeQuery C# (CSharp) Method

XSafeQuery() public static method

This provides a way to query the DB and do something with the results without having to copy the "try, open, try, execute, finally, close, finally, close" type logic in a bunch of places. This method correctly closes the objects used in the DB access in the event of an exception.
public static XSafeQuery ( AbstractSqlConnectionDescriptor connDesc, SqlTransaction transaction, string sql, IEnumerable sqlParams, DataReaderDelegate invokeMe, Hashtable parameters ) : void
connDesc AbstractSqlConnectionDescriptor The database connection descriptor. This is used both as /// a key for caching connections/commands as well as for /// getting the actual database connection the first time.
transaction SqlTransaction The transaction to do this as part of.
sql string The SQL statement to execute.
sqlParams IEnumerable A list of objects to use as parameters /// to the SQL statement. The list may be /// null if there are no parameters.
invokeMe DataReaderDelegate The method to delegate to. If null, nothing is /// done with the data reader and it is just closed.
parameters System.Collections.Hashtable The other parameters to the delegate, in whatever /// form makes sense for that delegate method.
return void
        public static void XSafeQuery(AbstractSqlConnectionDescriptor connDesc,
            SqlTransaction transaction, string sql,
            IEnumerable sqlParams, DataReaderDelegate invokeMe, Hashtable parameters)
        {
            IDbConnection conn = transaction != null
                ? transaction.Connection
                : DbCaches.Connections.Get(connDesc);
            try
            {
                IDbCommand cmd = DbCaches.Commands.Get(sql, conn);
                if (transaction != null)
                {
                    cmd.Transaction = transaction.Transaction;
                }
                try
                {
                    SetSQLOnCommand(connDesc, cmd, sql, sqlParams);
                    IDataReader reader;
                    try
                    {
                        reader = cmd.ExecuteReader();
                    }
                    catch (Exception e)
                    {
                        throw new UnableToRunSqlException(connDesc, sql, sqlParams, e);
                    }
                    try
                    {
                        if (invokeMe != null)
                        {
                            invokeMe(parameters, reader);
                        }
                    }
                    catch (ExceptionWithConnectionInfo)
                    {
                        // The delegate may have thrown an exception that came from another
                        // database utilities call, in which case we don't want to confuse
                        // the issue by wrapping with a different SQL command.
                        throw;
                    }
                    catch (Exception e)
                    {
                        throw new UnableToProcessSqlResultsException(connDesc, sql, sqlParams, e);
                    }
                    finally
                    {
                        try
                        {
                            reader.Close();
                        }
                        catch (Exception e)
                        {
                            // This exception is not rethrown because we don't want to mask any
                            // previous exception that might be being thrown out of "invokeMe".
                            _log.Warn("Caught an exception while trying to close the data reader.", e);
                        }
                    }
                }
                finally
                {
                    DbCaches.Commands.Return(sql, conn, cmd);
                }
            }
            finally
            {
                if (transaction == null)
                {
                    DbCaches.Connections.Return(connDesc, conn);
                }
            }
        }

Same methods

SqlConnectionUtilities::XSafeQuery ( AbstractSqlConnectionDescriptor connDesc, string sql, IEnumerable sqlParams, DataReaderDelegate invokeMe, Hashtable parameters ) : void