How to get ADO .NET command with parameters replaced with values

  • Thread starter Thread starter Andrus
  • Start date Start date
A

Andrus

I need to get sql command where parameters are replaced by values.
I tried code below but displayed sql statement contains parameter names,
not actual values.

How to obtain command where parameters are replaced by their values ?

Andrus.

static IDataReader ExecuteReader(string command
, out IDbConnection connection
, CommandBehavior behavior
, params IDbDataParameter[] dataParams)
{
connection = ...
connection.Open();
IDbCommand cmd = new SqlCommand(command, connection as
SqlConnection);
foreach (IDbDataParameter p in dataParams)
cmd.Parameters.Add(p);
// How to display replaced parameter values ?
MessageBox.Show(cmd.CommandText);
return cmd.ExecuteReader(behavior |
CommandBehavior.CloseConnection);
}
 
ADO.NET would never expect to need to do this (since it can pass
parameters, which is safer) - so I doubt there is anything built in.
You will probably need to do your own parse/replace (and escape).

Marc
 
Back
Top