Solved

How to get the actual SQL that will be executed from the command object....

Posted on 2006-07-01
5
208 Views
Last Modified: 2010-04-23
Dear All,

Probably a simple one. I want to display the actual SQL that gets executed ie with the parameter substituted.

My code is:

cmdDelete.Parameters("@CustomerID").Value = Me.m_CustomerID

con.Open()
Dim rowsAffected As Integer
MessageBox.Show("Delete SQL=" & m_CustomerID & ":" & cmdDelete.CommandText)
rowsAffected = cmdDelete.ExecuteNonQuery()

I get:

DELETE FROM Customers
WHERE     (CustomerID = [@CustomerID])

I want to get:

DELETE FROM Customers
WHERE     (CustomerID = 8)

How do I do this?

Thanks,

Sam
0
Comment
Question by:SamJolly
  • 3
5 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 17024057
what is m_CustomerID? a control on a form or a variable?

If it's a textbox, then try m_CustomerID.Text
0
 

Author Comment

by:SamJolly
ID: 17024363
YZlat,

Thanks for the comment. No I was expecting the command object to offer a property that would return what I need hence the attempted use of .CommandText. m-CustomerID is just a private variable within the Customer Class..

So is it possible to get the actual executed SQL from something in ADO.NET.

Thanks,

sam
0
 
LVL 34

Accepted Solution

by:
Sancler earned 500 total points
ID: 17024460
I don't think this is possible for the reason that, so far as I know, the CommandText with the Parameter.Value actually substituted for the parameter's marker never exists in ADO.NET.  My belief is that it is passed from .NET to the database driver with the statement in the form shown in your messagebox and with the parameter name/s and value/s passed separately.  I think it is the driver and/or the database itself that does the necessary substitution.  If that is correct, then the only way you will get a message on the lines you want is by "rolling your own".  In pseudo-code - which means I haven't tested it ;-) and it doesn't refer to a specific sort of Command, eg sql or oledb - you could do this with something like

   Dim msg As String = command.CommandText
   For Each param As Parameter In Command.Parameters
       msg = msg.Replace(param.ParameterName, param.Value.ToString)
   Next
   MsgBox(msg)

Roger
0
 

Author Comment

by:SamJolly
ID: 17026781
Roger,

So based on your advice I have created a helper function as such:

    Private Function GetSqlString(ByVal cmd As System.Data.OleDb.OleDbCommand)

        Dim strMessage As String = cmd.CommandText

        For Each param As OleDbParameter In cmd.Parameters
            strMessage = strMessage.Replace("[" & param.ParameterName & "]", param.Value.ToString)
        Next

        Return strMessage

    End Function

Thanks,

Sam
0
 

Author Comment

by:SamJolly
ID: 17026786
Thanks all for helping.

Sam
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now