我可以从 .NET 获取 SqlCommand 对象(带有 SQL 参数)生成的完整 SQL 字符串吗?
从 .NET 环境中,我可以访问由 SqlCommand 对象?
注意:在调试模式下,完整的 SQL 字符串会显示在 VisualStudio 中的 Intellisense 悬停中。
如果必须的话,我愿意使用反射技术。我确信这里有人知道一种方法来实现它。
更新 1:
我正在调用一个带有 cmd.CommandType = CommandType.StoredProcedure
参数的存储过程,并尝试获取生成并运行的完整 SQL。 我想知道 cmd 是否。Prepare() 方法可能将完整字符串存储在状态字段或类似字段中,则在这种情况下可能没有用处。
更新 2:
根据下面(和引用的)答案表明在准备或执行期间内部没有生成完整的 SQL 字符串,我使用 .NET Reflector 进行了一些探索。甚至内部连接类似乎也传递对象而不是将它们简化为字符串,例如:
internal abstract void AddPreparedCommand(SqlCommand cmd);
声明类型:System.Data.SqlClient.SqlInternalConnection
程序集:System.Data,版本=2.0.0.0
总的来说,感谢大家提供的详细信息,以证明可以做什么并显示实际发生的情况。非常感谢。我喜欢详尽的解释;它们增加了保证并为答案提供了可信度。
From the .NET environment can I get access to the full SQL string that is generated by a SqlCommand object?
Note: The full SQL string shows up in Intellisense hover, in VisualStudio, while in debug mode.
I'm willing to use reflection techniques if I must. I'm sure somebody here knows a way to get at it.
Update 1:
I'm calling a stored procedure having parameters with cmd.CommandType = CommandType.StoredProcedure
and am trying to acquire the full SQL generated and run.
I wonder if the cmd.Prepare() method might not prove useful in this circumstance, if it might store the full string in a state field or something like that.
Update 2:
In light of answers below (and referenced) that indicate no complete SQL string is generated internally during preparation or execution, I did a bit of poking around using .NET Reflector. Even the internal connection classes seem to pass objects rather than boiling them down to strings, for example:
internal abstract void AddPreparedCommand(SqlCommand cmd);
Declaring Type: System.Data.SqlClient.SqlInternalConnection
Assembly: System.Data, Version=2.0.0.0
In general, thanks to everybody for the level of detail you got into to prove what can be done and show what's actually happening. Much appreciated. I like thorough explanations; they add surety and lend credence to the answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一个简单的循环将所有参数名称替换为它们的值将为您提供类似于最终结果的结果,但存在几个问题。
这些值,并考虑到以相同字符开头的参数名称,例如
@NAME
和@NAME_FULL
,我们可以将所有参数名称替换为该参数位置的值:然而,这还剩下一个问题,那就是如果参数是一个字符串,那么最初看起来像这样的 SQL:
将看起来像这样:
这显然不是合法的 SQL,所以我们需要考虑一些参数 -类型以及:
A simple loop replacing all the parameter names with their values will provide you with something similar to what the end result is, but there are several problems.
With those in place, and taking into account parameter names that starts with the same characters, like
@NAME
and@NAME_FULL
, we can replace all the parameter names with the value that would be in the place of that parameter:there is one problem left with this, however, and that is if a parameter is a string, then the SQL that initially looks like this:
will look like this:
This is clearly not legal SQL, so we need to account for some parameter-types as well:
这里有几个类似的问题。
针对这个问题提供了最令人信服的答案:如何从 SqlCommand-Object 获取生成的 SQL-Statment?
答案是:
There have been a couple of similar questions here.
The most compelling answer was provided to this question: How to get the generated SQL-Statment from a SqlCommand-Object?
and the answer was:
命令上的
CommandText
属性(或调用ToString()
)将为您提供所有 SQL,但有一个小例外。它肯定会给您在调试器中看到的任何内容。请注意,这不会为您提供参数值,但它会为您提供实际的命令。唯一需要注意的是,当 CommandType 为 Text 时,ADO.NET 框架经常(事实上,几乎总是)使用 sp_executesql 来执行命令而不是直接针对连接执行命令。从这个意义上说,不可能获得执行的准确 SQL。
The
CommandText
property (or callingToString()
) on your command will give you all of the SQL, with a small exception. It will definitely give you anything you see in the debugger. Note that this won't give you parameter values, but it will give you the actual command.The only caveat is that when
CommandType
isText
, the ADO.NET framework will often (in fact, almost always) usesp_executesql
to execute the command rather than executing the command directly against the connection. In that sense, it's not possible to obtain the exact SQL that gets executed.我还没有尝试过这个,但如果您愿意使用 SMO,您也许可以使用捕获模式:
http://msdn.microsoft.com/en-us/library/ms162182(v=sql.120).aspx
I haven't tried this, but you may be able to use Capture Mode if you are willing to use SMO:
http://msdn.microsoft.com/en-us/library/ms162182(v=sql.120).aspx
我喜欢耶稣拉莫斯的回答,但我需要输出参数的支持。 (我还使用了字符串生成器来生成内容。)
声明输出参数的参数
构建主参数区域
列出输出参数结果
完整代码:
I like Jesus Ramos answer, but I needed support for output parameters. (I also used a string builder to generate the content.)
Declare Parameter for output parameters
Build Main Parameter Area
List Output Parameter results
Full Code: