Example #1
0
        /// <summary>
        /// Executes this command using the specified database connection to get a data reader and then executes the specified method with the reader.
        /// </summary>
        public void Execute(DBConnection cn, Action <DbDataReader> readerMethod)
        {
            var command = cn.DatabaseInfo.CreateCommand();

            command.CommandText =
                "SELECT{0} {1} ".FormatWith(
                    cacheQueryInDatabase && cn.DatabaseInfo.QueryCacheHint.Any() ? " {0}".FormatWith(cn.DatabaseInfo.QueryCacheHint) : "",
                    StringTools.ConcatenateWithDelimiter(", ", selectExpressions.ToArray())) + fromClause;

            if (conditions.Any())
            {
                command.CommandText += " WHERE ";
                var first       = true;
                var paramNumber = 0;
                foreach (var condition in conditions)
                {
                    if (!first)
                    {
                        command.CommandText += " AND ";
                    }
                    first = false;
                    condition.AddToCommand(command, cn.DatabaseInfo, InlineUpdate.GetParamNameFromNumber(paramNumber++));
                }
            }

            command.CommandText = command.CommandText.ConcatenateWithSpace(orderByClause);
            cn.ExecuteReaderCommand(command, readerMethod);
        }
Example #2
0
        /// <summary>
        /// Executes this command against the specified database connection and returns the number of rows affected.
        /// </summary>
        public int Execute(DBConnection cn)
        {
            if (conditions.Count == 0)
            {
                throw new ApplicationException("Executing an inline delete command with no parameters in the where clause is not allowed.");
            }
            var command = cn.DatabaseInfo.CreateCommand();

            command.CommandText = "DELETE FROM " + tableName + " WHERE ";
            var paramNumber = 0;

            foreach (var condition in conditions)
            {
                condition.AddToCommand(command, cn.DatabaseInfo, InlineUpdate.GetParamNameFromNumber(paramNumber++));
                command.CommandText += " AND ";
            }
            command.CommandText = command.CommandText.Remove(command.CommandText.Length - 5);
            return(cn.ExecuteNonQueryCommand(command));
        }