Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "final" prepared statement from MySqlCommand

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?

like image 558
slkandy Avatar asked Jun 09 '26 12:06

slkandy


2 Answers

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

like image 57
Craig Blake Avatar answered Jun 12 '26 00:06

Craig Blake


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.

like image 43
leeeroy Avatar answered Jun 12 '26 00:06

leeeroy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!