?
Solved

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

Posted on 2006-07-01
5
Medium Priority
?
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

801 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