Note that there are some explanatory texts on larger screens.

plurals
  1. POParameterized Queries (C#, Oracle): How to produce a more readable representation?
    primarykey
    data
    text
    <p>I am using parameterized queries in my C# code to interact with an Oracle database. What can I do to log the statements in a more readable fashion?</p> <p>Suppose I have a parameterized query like:</p> <pre><code>INSERT INTO PERSON (ID, NAME, BIRTHDATE) VALUES (:id, :name, :birthdate) </code></pre> <p>Ideally I would like to see the log entry with all parameters replaced so I could copy and paste the statement for later use:</p> <pre><code>INSERT INTO PERSON (ID, NAME, BIRTHDATE) VALUES (23, ‘Mike’, TO_DATE('2003/07/09', 'yyyy/mm/dd') </code></pre> <p>My current approach is to print out the string of the parameterized query and then iterate over all parameters and use ToString(). This is a bit hard to read, if there are many parameters. It would yield something like:</p> <pre><code>INSERT INTO PERSON (ID, NAME, BIRTHDATE) VALUES (:id, :name, :birthdate) [:id=23, :name=Mike, birthdate=2004/07/09 00:00:00] </code></pre> <p>Another approach I am planning would be to use the string.Replace() function to replace the parameter placeholders. But maybe there some better way to do this?</p> <p>Thanks in advance.</p> <p><strong>Edit 1:</strong></p> <p>I thought better to provide some code example.</p> <p>I am using parameterized queries in this form (Note: I am using log4net):</p> <pre><code> using (OracleConnection connection = new OracleConnection(connectionString)) using (OracleCommand command = new OracleCommand(statement, connection)) { command.Parameters.AddWithValue(":id", id); command.Parameters.AddWithValue(":name", name); command.Parameters.AddWithValue(":birthdate", birthdate); command.Connection.Open(); log.DebugFormat("Executing statement: {0}.", command.CommandText); // there I would add some more code to iterate over // the parameters and print them out command.ExecuteNonQuery(); command.Connection.Close(); } </code></pre> <p>I am looking for a way to log out the statement, which the oracle command object is using. My current approaches (see question) are yet not very satisfying, because not very readable.</p> <p>I hoped that there would be some API (maybe something even in the OracleClient namespace) that would help to parse the parameterized query for me. I could do some more sophisticated string replacement or regex, but I wanted to collect some knowledge. I have already done some reasearch on it, but didn't found something.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload