简体   繁体   中英

Catch a query string with parameter values from a SqlDataSource in ASP.net applications

In my ASP.net application I have an SqlDataSource with parameters eg:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="..." 
    onselecting="SqlDataSource1_Selecting" ProviderName="System.Data.SqlClient" 
    SelectCommand="SELECT * FROM [SomeTable] WHERE ([SomeTableID] = @SomeTableID)">
    <SelectParameters>
        <asp:ControlParameter ControlID="DropDownList1" Name="SomeTableID" 
            PropertyName="SelectedValue" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

For debugging purposes it would be very helpful to somehow catch the query string which is executed on the database server ie the query string with filled in parameter values.

I tried to do this in the SqlDataSource1_Selecting event, but the SqlDataSourceSelectingEventArgs arguments still contain the SelectCommand text as above: SELECT * FROM [SomeTable] WHERE ([SomeTableID] = @SomeTableID) .

How can I get the true query string with all @Valiables replaced by the values which are used for the selection?

There is no way to view it in code. The SqlCommand class calls sp_execute which separates the query from the parameters. You'll need to use Sql Profiler to see the exact query executed.

You can learn how to use SqlProfiler through this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM