查看参数替换后的精确sql C#
有没有办法在 CommandText 执行后立即访问它? 我有以下代码:
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);
我需要知道的是在数据库中执行的sql命令字符串,该字符串已经包含在sql中parmValue的值。换句话说,我不能看到查询中的字符串“@parm”,而是看到它的值。
我需要这个,以便我可以将此 sql 推送到日志数据库中以供将来参考。
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定 Oracle,但在我个人使用的 RDBMS 中,这是不可能完成的。
当我需要获取参数并查看生成的 SQL 查询时,我总是通过循环遍历代码中的参数集合并提取名称/值对以将生成的查询或类似的内容记录到我们的错误日志记录中来完成此操作数据库:
我只在错误日志记录中执行此操作,因为每次调用时执行此操作都会降低性能,但它也可以在测试场景中工作。
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:
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.
我不确定 Oracle 是否完全按照这种方式工作,但对于大多数其他数据库提供程序来说,参数化查询不会作为纯文本查询发送到服务器,而是作为带有适当参数的执行命令发送。换句话说,在应用程序级别没有发生替换,仅在数据库本身发生替换。要查看在服务器上执行的实际命令,您可能需要查看数据库本身的跟踪/日志。
I am not certain if Oracle works exactly in this way, but for most other DB providers, a parameterized query is not sent to the server as a plain text query, but is instead sent as a an Execute command with proper parameters. In other words there is no substitution happening at the level of your application, only in the DB itself. To view the actual command as they are being executed on the server, you may want to look at a trace/log of the database itself.