Is it possible to see the sql statement after parameters have been replaced?
using(SqlCommand cmdInsert = new SqlCommand("INSERT INTO Table(Value_fkey) VALUES(@ValueKey)", Database.Connection))
{
cmdInsert.Parameters.AddWithValue("@ValueKey", ValueKey);
System.Convert.ToInt32(cmdInsert.ExecuteScalar());
}
I'd like it to log my sql statements, so rather not via SQL Server, but I don't mind whether it is before, or after calling the statement.
This seems to be the simplest way to do it then:
public void OutputSQLToTextFile(SqlCommand sqlCommand)
{
string query = sqlCommand.CommandText;
foreach (SqlParameter p in sqlCommand.Parameters)
{
query = query.Replace(p.ParameterName, p.Value.ToString());
}
OutputToTextFile(query);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With