http://stackoverflow.com/questions/265192/get-the-generated-sql-statement-from-a-sqlcommand-object (answer by Mitch)
Ejemplo n.º 1
0
        /// <summary>
        /// Logs the command.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="command">The command.</param>
        /// <param name="logger">Logger</param>
        public static void LogCommand(string type, IDbCommand command, ILogger logger)
        {
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            try
            {
                if (command is SqlCommand sqlCmd)
                {
                    logger.LogDebug("{0}{1}{2}", Environment.NewLine, SqlCommandDumper.GetCommandText(sqlCmd));
                    return;
                }

                StringBuilder sb = new StringBuilder((command.CommandText ?? "").Length + 1000);
                sb.Append(type);
                sb.Append("\r\n");
                sb.Append(command.CommandText);
                if (command.Parameters != null && command.Parameters.Count > 0)
                {
                    sb.Append(" --- PARAMS --- ");
                    foreach (DbParameter p in command.Parameters)
                    {
                        sb.Append(p.ParameterName);
                        sb.Append("=");
                        if (p.Value == null || p.Value == DBNull.Value)
                        {
                            sb.Append("<NULL>");
                        }
                        else
                        {
                            sb.Append(p.Value.ToString());
                        }
                        sb.Append(" ");
                    }
                }

                logger.LogDebug(sb.ToString());
            }
            catch (Exception ex)
            {
                logger.LogDebug("Error logging command: " + ex.ToString());
            }
        }
Ejemplo n.º 2
0
        public static void LogCommand(string type, IDbCommand command)
        {
            try
            {
                var sqlCmd = command as SqlCommand;
                if (sqlCmd != null)
                {
                    Log.Debug(type + "\r\n" + SqlCommandDumper.GetCommandText(sqlCmd));
                    return;
                }

                StringBuilder sb = new StringBuilder((command.CommandText ?? "").Length + 1000);
                sb.Append(type);
                sb.Append("\r\n");
                sb.Append(command.CommandText);
                if (command.Parameters != null && command.Parameters.Count > 0)
                {
                    sb.Append(" --- PARAMS --- ");
                    foreach (DbParameter p in command.Parameters)
                    {
                        sb.Append(p.ParameterName);
                        sb.Append("=");
                        if (p.Value == null || p.Value == DBNull.Value)
                        {
                            sb.Append("<NULL>");
                        }
                        else
                        {
                            sb.Append(p.Value.ToString());
                        }
                        sb.Append(" ");
                    }
                }

                Log.Debug(sb.ToString());
            }
            catch (Exception ex)
            {
                Log.Debug("Error logging command: " + ex.ToString());
            }
        }