简体   繁体   中英

Getting null result from SQL Server stored proc which returns XML

I have a stored proc which returns a piece of XML. It works fine when I execute it from MS SQL Management Studio. I need to write some C# code to execute the proc and take the resulting XML and write it to a file. I wrote the code and it executes the proc (I checked using the profiler), and there is no error, but the XmlReader is empty. My code is something like:

using (SqlConnection c = new SqlConnection(-snip-))
{
c.Open();
SqlCommand myCommand = new SqlCommand("sp_formfill_contract_xml", c);

myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@i_contract_id", frm_contract_id.Text.ToString());                    

System.Xml.XmlReader xmlr = myCommand.ExecuteXmlReader();
xmlr.Read();


while (xmlr.ReadState != System.Xml.ReadState.EndOfFile)
    {
    MessageBox.Show(xmlr.ReadOuterXml());
    // do stuff with the XML snippet here
    }

    c.Close();
    }

As a comparison test I created a 2nd stored proc which returns a plain string containing the XML that the 1st proc generates, and the C# code receives that just fine, so long as I don't use the XML reader. So it's something to do with the kind of data that the XML proc is returning which makes it appear empty.

Any ideas? -Sean

I dont see the reason why it should not work my only guess is that you must had one row and you already went to endoffile calling xmlr.Read(); so please try the below way

using(System.Xml.XmlReader xmlr = myCommand.ExecuteXmlReader())
{
 while xmlr.Read())
 {
    MessageBox.Show(xmlr.ReadOuterXml());
 }
}

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