I am using parameterized queries in my C # code to interact with an Oracle database. What can I do to write statements in a more readable way?
Suppose I have a parameterized query like:
INSERT INTO PERSON (ID, NAME, BIRTHDATE) VALUES (:id, :name, :birthdate)
Ideally, I would like to see a log entry with all parameters replaced, so that I can copy and paste the statement for later use:
INSERT INTO PERSON (ID, NAME, BIRTHDATE) VALUES (23, ‘Mike’, TO_DATE('2003/07/09', 'yyyy/mm/dd')
My current approach is to print a parameterized query string and then iterate over all the parameters and use ToString (). It is a little difficult to read if there are many parameters. This will give something like:
INSERT INTO PERSON (ID, NAME, BIRTHDATE) VALUES (:id, :name, :birthdate) [:id=23, :name=Mike, birthdate=2004/07/09 00:00:00]
, , string.Replace() . , ?
.
1:
, .
(: log4net):
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);
command.ExecuteNonQuery();
command.Connection.Close();
}
, oracle. (. ) , .
, API (, - OracleClient), . , . , .