Ejemplo n.º 1
0
    protected T ExecuteScalar <T>(SqlCommand cmd, SqlTransaction transaction)
    {
        lock (Connection)
        {
            try
            {
                if (cmd.Connection == null)
                {
                    cmd.Connection = Connection;
                }

                if (transaction != null)
                {
                    cmd.Transaction = transaction;
                }

                OpenConnection(cmd.Connection);

                var ret = cmd.ExecuteScalar();

                return(ReferenceEquals(ret, DBNull.Value) ? default(T) : ObjectUtility.CastMe <T>(ret));
            }
            finally
            {
                if (AutoCloseConnection)
                {
                    CloseConnection(cmd.Connection);
                }
            }
        }
    }