Skip to content

Instantly share code, notes, and snippets.

@jsramraj
Last active July 23, 2020 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsramraj/537fd08c7078ca14352e8fd31551495f to your computer and use it in GitHub Desktop.
Save jsramraj/537fd08c7078ca14352e8fd31551495f to your computer and use it in GitHub Desktop.
Get Printable sql query from the SQLiteCommand in C#
private string ExtractSqlCommandFromCommand(SQLiteCommand cmd)
{
string sql = cmd.CommandText;
bool first = true;
foreach (SQLiteParameter p in cmd.Parameters)
{
string value = ((p.Value == DBNull.Value) ? "null"
: (p.Value is string) ? "'" + p.Value + "'"
: (p.Value is DateTime) ? "'" + ((DateTime)p.Value).ToString("yyyy-MM-dd HH:mm:ss") + "'"
: p.Value.ToString());
int pos = sql.IndexOf("?");
sql = sql.Substring(0, pos) + value + sql.Substring(pos + 1);
/*
//use this logic, if you used string formatter rather than the '?'
if (first)
{
sql += string.Format(" {0}={1}", p.ParameterName, value);
first = false;
}
else
{
sql += string.Format("\n , {0}={1}", p.ParameterName, value);
}
*/
}
return sql;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment