Having SQL with parameter (passing through SelectCommand.Parameters.AddWithValue
),
can I get easily final SQL to run it immediately? I mean, not to have unresolved parameters in the command, but rather fully resolved SQL. I can do that manually by Replace function, but I have to deal with types, etc. So just wondering if there is a way to grab the SQL statement processed by SQL engine directly (something what I can see in SQL Profiler). E.g. to have
SELECT Name FROM TABLE WHERE Id = 15
instead of
SELECT Name FROM TABLE WHERE Id = @Id
I need to log whole SQL that is runnable copy&paste&run. I can log command and parameters, but in this approach I have to manually construct the SQL statement.
DataTable dt = new DataTable();
query = "SELECT Name FROM TABLE WHERE Id = @Id";
using (SqlDataAdapter da = new SqlDataAdapter(query, String.Format(@"Data Source={0};Initial Catalog={1};Integrated Security=SSPI", relServer, relDatabase)))
{
if (QueryParams != null && QueryParams.Count > 0)
{
foreach (KeyValuePair<string, object> entry in QueryParams)
{
da.SelectCommand.Parameters.AddWithValue(entry.Key, entry.Value ?? DBNull.Value);
}
}
da.Fill(dt);
}