Skip to content

Instantly share code, notes, and snippets.

@jamesrcounts
Created September 7, 2012 17:11
Show Gist options
  • Select an option

  • Save jamesrcounts/3667853 to your computer and use it in GitHub Desktop.

Select an option

Save jamesrcounts/3667853 to your computer and use it in GitHub Desktop.
A text formatter for SqlCommand instances
/// <summary>
/// Formats the command.
/// </summary>
/// <param name="command">The command.</param>
/// <returns>A formatted <see cref="SqlCommand"/></returns>
private static string FormatCommand(SqlCommand command)
{
string result = command.CommandText + Environment.NewLine;
result = string.Format("{0}{{{1}", result, Environment.NewLine);
foreach (var item in command.Parameters)
{
result = result + new string(' ', 4);
var p = item as SqlParameter;
result = p == null ?
result + "NULL" :
result + string.Join(", ", p.ParameterName, p.SqlDbType, p.Size, p.Value);
result = result + Environment.NewLine;
}
result = string.Format("{0}}}{1}", result, Environment.NewLine);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment