C#Get the generated SQL statement from a SqlCommand object?

For logging purposes, I'm afraid there's no nicer way of doing this but to construct the string yourself:

string query = cmd.CommandText;

foreach (SqlParameter p in cmd.Parameters)
{
    query = query.Replace(p.ParameterName, p.Value.ToString());
}
原文地址:https://www.cnblogs.com/grj001/p/12223663.html