oledb/ado.net:获取命令文本,替换所有参数 [英] oledb/ado.net: Get the command's text, with all parameters replaced
问题描述
是否可以获得用所有参数替换为其值的OleDbCommand
的文本?例如.在下面的代码中,我正在寻找一种获取查询文本的方法
Is it possible to get the text of an OleDbCommand
with all parameters replaced with their values? E.g. in the code below I'm looking for a way to get the query text
SELECT * FROM my_table WHERE c1 = 'hello' and c2 = 'world'
完成参数分配后.
var query = "SELECT * FROM my_table WHERE c1 = ? and c2 = ?";
var cmd = new OleDbCommand(query, connection);
cmd.Parameters.Add("@p1", OleDbType.WChar).Value = "hello";
cmd.Parameters.Add("@p2", OleDbType.WChar).Value = "world";
推荐答案
否:您必须自己遍历参数集合,并执行string.Replace()以获得等效项.当您必须使用?
语法而不是@parametername
语法时,这特别痛苦.
No: you have to iterate through the parameters collection yourself, doing a string.Replace() to get the equivalent. It's particularly painful when you have to use the ?
syntax rather than the @parametername
syntax.
这样做的原因是完整的字符串是从不组装的.这些参数并发送到服务器并视为数据,并且永远不会包含在字符串中.
The reason for this is that the full string is never assembled. The parameters and sent to the server and treated as data, and are never included in the string.
都一样,我一个人了解你的痛苦.如果它们包含某种您可以调用用于调试目的的.ComposeSQL()
方法,那会很好,这也许还会产生一个编译器警告,以帮助避免在生产中使用.
All the same, I for one understand your pain. It would have been nice if they included some kind of .ComposeSQL()
method you could call for debugging purposes, that perhaps also produces a compiler warning to help avoid use in production.
这篇关于oledb/ado.net:获取命令文本,替换所有参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!