获得“最终”结果来自 MySqlCommand 的准备好的语句

发布于 2024-08-02 22:10:35 字数 569 浏览 8 评论 0原文

我有以下 MySqlCommand:

Dim cmd As New MySqlCommand
cmd.CommandText = "REPLACE INTO `customer` VALUES( ?customerID, ?firstName, ?lastName)"

With cmd.Parameters
 .AddWithValue("?customerID", m_CustomerID)
 .AddWithValue("?firstName", m_FirstName)
 .AddWithValue("?lastName", m_LastName)
End With

我有一个处理 MySqlCommands 执行的类,我希望它将每个查询记录到文件中。我可以检索正在执行的查询/命令:

cmd.CommandText

但这只是返回带有参数(?customerID,?firstName等)的原始CommandText,而不是由添加的实际替换值AddWithValue 函数。如何找出实际执行的“最终”查询?

I have the following MySqlCommand:

Dim cmd As New MySqlCommand
cmd.CommandText = "REPLACE INTO `customer` VALUES( ?customerID, ?firstName, ?lastName)"

With cmd.Parameters
 .AddWithValue("?customerID", m_CustomerID)
 .AddWithValue("?firstName", m_FirstName)
 .AddWithValue("?lastName", m_LastName)
End With

I have a class that handles execution of MySqlCommands and I'd like to have it log every query to a file. I can retrieve the query/command being executed with:

cmd.CommandText

but that just returns the original CommandText with the parameters (?customerID, ?firstName, etc.) and not the actual substituted values added by the AddWithValue functions. How can I find out the actual "final" query that was executed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不爱素颜 2024-08-09 22:10:35

我做了以下操作:

dim tmpstring as string = MySqlCommand.CommandText
For each p as MySqlParameter in MySqlCommand.parameters
    tmpstring = tmpstring.replace(p.ParameterName, p.Value)
Next

这似乎输出了您需要的一切

I did the following:

dim tmpstring as string = MySqlCommand.CommandText
For each p as MySqlParameter in MySqlCommand.parameters
    tmpstring = tmpstring.replace(p.ParameterName, p.Value)
Next

This seems to output everything you need

ˇ宁静的妩媚 2024-08-09 22:10:35

我还没有看到这方面的方法。

在任何情况下,准备好的语句都会使用 ?customerID,?firstname 参数发送到服务器,然后单独发送实际参数 - mysql 驱动程序不会像您那样构建最终的 sql 查询不要使用准备好的语句。

I havn't seen a method for this.

And in any case, prepared statements are sent to the server with the ?customerID,?firstname parameters, and then the actual parameters are sent seperately - the mysql driver doesn't build up a final sql query like you'd do if you didn't use prepared statements.

ゝ杯具 2024-08-09 22:10:35

您使用的参数化方法应该可以防止 SQL 注入。

.AddWithValue("?customerID", m_CustomerID)

如果 m_CustomerID 包含文本

Haha I'm stealing your data; drop table whatever;

那么它最终不会在服务器上执行。 AddWithValue 会为您解决这个问题。

至于实际执行的查询,如果启用了,您应该能够从查询日志中获取它。

The parameterised method you're using should be okay for preventing SQL injection.

.AddWithValue("?customerID", m_CustomerID)

If m_CustomerID contains the text

Haha I'm stealing your data; drop table whatever;

Then it won't end up being executed on the server as such. The AddWithValue sorts that out for you.

As for the actual executed query, you should be able to get that from the query-log, if it's enabled.

梅倚清风 2024-08-09 22:10:35

您必须自己构建它。

参数不只是放入字符串中然后作为 SQL 语句运行。 RDBMS 将实际准备 SQL,然后根据需要使用参数值。因此,没有一条 SQL 语句发送到服务器。要查看 SQL 是什么,您必须执行以下操作:

Console.WriteLine("REPLACE INTO `customer` VALUES('" & m_CustomerID & _
    "', '" & m_FirstName & "', '" & m_LastName & "')")

You would have to build it yourself.

Parameters are not just plopped into a string and then run as a SQL statement. The RDBMS will actually prepare the SQL and then use the parameter values as needed. Therefore, there's not a single SQL statement going to the server. To see what the SQL would be, you would have to do:

Console.WriteLine("REPLACE INTO `customer` VALUES('" & m_CustomerID & _
    "', '" & m_FirstName & "', '" & m_LastName & "')")
遇到 2024-08-09 22:10:35

我有同样的需要。

根据我的阅读,查询文本不会与客户端中的参数值相结合 - 它们会被发送到服务器。

为了检查实际发送到服务器的查询,我使用了 mysqld 日志记录。对于我的 MySQL 版本,我将此条目添加到 my.cnf:

log=queries.txt

然后,我能够清楚地看到将命令文本与参数相结合的效果: 在我的例子中,重新启动 mysqld 后,我运行了单元测试,然后打开querys.txt 文件。

哈!

I have the same need.

From what I've read, the query text isn't combined with the param values in the client - they are sent to the server for that.

To inspect what query was actually being sent to the server, I used mysqld logging. For my version of MySQL, I added this entry to the my.cnf:

log=queries.txt

Then, I was able to see clearly the effect of combining command text with parameters: in my case, after restarting the mysqld, I ran my unit tests and then opened the queries.txt file.

HTH!

感受沵的脚步 2024-08-09 22:10:35

如果您想从 .NET 应用程序自行管理日志记录,最好的选择是继续使用带参数的 MySqlCommand 类以避免 SQL 注入;但是,当您记录 CommandText 时,请循环遍历参数集合并按名称/类型/值记录每个参数。

If you want to manage logging yourself from the .NET application, your best bet is to continue using the MySqlCommand class with parameters to avoid SQL injection; however, when you log the CommandText, loop through the Parameters collection and log each one by name/type/value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文