Last active
July 23, 2020 12:20
-
-
Save jsramraj/537fd08c7078ca14352e8fd31551495f to your computer and use it in GitHub Desktop.
Get Printable sql query from the SQLiteCommand in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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