Solved

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

Posted on 2006-07-01
5
210 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need a starter for ETL protocol? 4 57
Get size of each directory on each mapped drive 5 29
I need help converting a bitmap to an image in VB.Net 1 33
write xml in vb.net 2 24
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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

832 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