我在 C# 代码中使用参数化查询来与 Oracle 数据库交互。我该怎么做才能以更易读的方式记录语句?
假设我有一个参数化查询,例如:
INSERT INTO PERSON (ID, NAME, BIRTHDATE) VALUES (:id, :name, :birthdate)
理想情况下,我希望看到所有参数都被替换的日志条目,以便我可以复制并粘贴该语句以供以后使用:
INSERT INTO PERSON (ID, NAME, BIRTHDATE) VALUES (23, ‘Mike’, TO_DATE('2003/07/09', 'yyyy/mm/dd')
我当前的方法是打印出参数化查询的字符串,然后迭代所有参数并使用 ToString()。如果有很多参数,这有点难以阅读。它会产生类似的结果:
INSERT INTO PERSON (ID, NAME, BIRTHDATE) VALUES (:id, :name, :birthdate) [:id=23, :name=Mike, birthdate=2004/07/09 00:00:00]
我计划的另一种方法是使用 string.Replace() 函数来替换参数占位符。但也许有更好的方法来做到这一点?
提前致谢。
Edit 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);
// there I would add some more code to iterate over
// the parameters and print them out
command.ExecuteNonQuery();
command.Connection.Close();
}
我正在寻找一种方法来注销 oracle 命令对象正在使用的语句。我目前的方法(参见问题)还不是很令人满意,因为可读性不是很好。
我希望有一些 API(甚至可能在 OracleClient 命名空间中)可以帮助我解析参数化查询。我可以做一些更复杂的字符串替换或正则表达式,但我想收集一些知识。我已经对此做了一些研究,但没有发现任何东西。