Search code examples
c#sqlloggingoraclecommand

viewing exact sql after parameter substitution C#


Is there a way to access the CommandText just after it gets executed? I have the following code:

cmd = new OracleCommand(sql.ToString(), conn);
cmd.Parameters.Add(new OracleParameter("@parm", parmValue));


OracleDataAdapter da = new OracleDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);

What I need to know is the sql command string executed in the database that already has parmValue's value contained in the sql. In other words, I must not be able to see the string "@parm" inside the query but the value of it instead.

I need this so that I could push this sql in a log database for future reference.

Thanks in advance.


Solution

  • I'm not sure about Oracle, but in the RDBMS's I work with personally this can't be done.

    When I've needed to get the parameters and view the resulting SQL Query I've always done it by looping through the parameter collection in the code and extracting the Name/Value pairs to log the resulting query or something like this into our error logging database:

    StoredProc=InsertCallRecord: Parameters: CallId=1234;Custid=651;PersonCalling=Dave...

    I only do this on error logging because it's a performance drag to do it on every call, but it could work in a test scenario as well.