如何获得实际上是由一个参数化查询。NET(VB或者C#)执行的SQL?是由、如何获得、参数、SQL

2023-09-04 01:26:45 作者:野战霸王

有没有一种简单的方法来查看由的DbCommand 对象对底层数据库实际执行的SQL命令文本(即之后的参数都被处理成一个语句) ?

Is there a straight forward way to view the SQL command text actually executed against the underlying database by a DbCommand object (i.e. after the parameters have been processed into a statement)?

下面的详细信息:我使用VB.Net 3.5,有一个工厂对象( DbProviderFactory )和连接( System.Data这。的IDbConnection )。我现在用的是工厂创建使用 DbProviderFactory.CreateCommand()方法一个新的命令对象,然后分配一个参数化查询到的CommandText 命令的财产。后来我创建并关联与查询的一些参数(使用创建的 DbProviderFactory )。

Here the detail: I'm using VB.Net 3.5 and have a factory object (DbProviderFactory) and a connection (System.Data.IDbConnection). I am using the factory to create a new command object using the DbProviderFactory.CreateCommand() method and then assigning a parametrized query to the CommandText property of the command. Later on I create and associate some parameters with the query (created using the DbProviderFactory).

这是伟大的,但我想看看通过命令对象生成实际的SQL语句。我目前正在测试这种对SQLite数据库,但不知道是否有一种方法,能工作的一般方法。

This is great, but I want to get a look at the actual SQL statement generated by the command object. I am currently testing this against a SQLite database, but wondered if there were a general approach which would work.

推荐答案

由服务器执行的实际SQL是你写什么。这些参数与命令本身分开发送,所以他们实际上从未被替换为它们在SQL指令值。

The actual SQL executed by the server is exactly what you wrote. The parameters are sent separately from the command itself, so they're actually never replaced with their value in the SQL command.