/// <summary>
        /// Executes the query, and returns the first column of the first row in the result set returned by the query.
        /// Additional columns or rows are ignored.
        /// </summary>
        /// <typeparam name="T">Type of model.</typeparam>
        /// <param name="query">The query.</param>
        /// <returns>
        /// The first column of the first row in the result set, or <see langword="null"/> if the result set is empty.
        /// Returns a maximum of 2033 characters.
        /// </returns>
        /// <exception cref="ArgumentNullException">If query is null.</exception>
        public object ExecuteScalar <T>(IQuery <T> query)
        {
            Check.NotNull(query, nameof(query));

            using (var cnHelper = OpenConnection())
                using (DbCommandInfo commandInfo = CreateCommand(query.Expression))
                {
                    _logger.LogCommand(commandInfo.Command);
                    if (commandInfo.Reader == null)
                    {
                        return(commandInfo.Command.ExecuteScalar());
                    }
                    else
                    {
                        using (IDataReaderEnvelope reader = commandInfo.Reader)
                        {
                            reader.SetInnerReader(commandInfo.Command.ExecuteReader());
                            if (reader.Read())
                            {
                                return(reader.GetValue(0));
                            }
                        }
                    }
                    return(null);
                }
        }
Example #2
0
 public QueryDataReader(IDbCommand command, IDataReaderEnvelope readerEnvelope, bool closeConnectionWhenFinished)
 {
     Check.NotNull(command, nameof(command));
     _command = command;
     if (readerEnvelope == null)
     {
         _reader = _command.ExecuteReader();
     }
     else
     {
         readerEnvelope.SetInnerReader(_command.ExecuteReader());
         _reader = readerEnvelope;
     }
     _closeConnectionWhenFinished = closeConnectionWhenFinished;
 }
 public DbCommandInfo(DbCommand command, IDataReaderEnvelope reader)
 {
     Command = command;
     Reader  = reader;
 }
Example #4
0
 /// <summary>
 /// Creates an instance with specified <paramref name="query"/> and <paramref name="reader"/>.
 /// </summary>
 /// <param name="query">SQL query.</param>
 /// <param name="reader">Reader used for query, if database engine itself cannot handle query itself.</param>
 public QueryInfo(string query, IDataReaderEnvelope reader)
 {
     Query  = query;
     Reader = reader;
 }