Search code examples
c#sqlxml

view SqlXmlCommand.ExecuteCommand() on SQL server


I am having an issue where I am looking at a legacy application that is using SqlXmlCommand objects to get data from the database. There is an .xsd file that has the tables that are being used, and what fields, their relationships etc. The issue that we are having is it works most of the time, but not all. I am wondering if there is a way to check what is actually being run on Sql Server. I don't have the SQL profiler installed so that option is out.

the code looks like:

SqlXmlCommand xcmd = new SqlXmlCommand(DataAccess.OleDbConnectionString);
xcmd.CommandType = SqlXmlCommandType.XPath;
xcmd.SchemaPath = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"myXsd.xsd"));
xcmd.XslPath = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, String.Format(@"myXsl.xsl", ReportType)));
xcmd.CommandText = "id[@PK=$PK]";
SqlXmlParameter p = xcmd.CreateParameter();
p.Name = "@PK";
p.Value = Id;

using (Stream s = xcmd.ExecuteStream()) { ... }

This blows up at the ExectureStream() with the error:

SQLXML: error loading XML result (XML document must have a top level element.)

We believe that there is some data abnormality that is causing the xml to not generate properly, and that is why we want to see what is exactly run.

Cheers


Solution

  • You can try the below two queries, you might need to tweak it a little, but to give you an idea, the first gives you a list of all requests, and the second will give you the detail of the request by its request id (session_id)

    SELECT *
    FROM sys.dm_exec_requests
    
    DBCC INPUTBUFFER (12345)
    

    Although I would personally rather try and debug the C# app first and view what's being sent over to the server from the VS debugger before bothering with checking what's being run on SQL Server

    Also, DBCC INPUTBUFFER might give you something like EXECUTE dbo.MyStoredProc 'params...', to dig deeper, or otherwise a more straightforward query, you can run this

    SELECT r.session_id, r.[status], r.command, t.[text]
    FROM sys.dm_exec_requests r
    CROSS APPLY sys.dm_exec_sql_text(r.[sql_handle]) t