我有一個存儲過程返回一段XML。當我從MS SQL Management Studio執行它時,它工作正常。 我需要編寫一些C#代碼來執行proc,並生成XML並將其寫入文件。 我寫了代碼,它執行proc(我使用profiler檢查),並且沒有錯誤,但XmlReader是空的。 我的代碼是這樣的:從SQL Server存儲過程獲得空結果存儲過程,返回XML
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();
}
作爲對比測試我創建了一個第二存儲過程返回包含該第一PROC生成XML純字符串,和C#代碼接收就好,只要我不使用XML閱讀器。所以這與XML proc返回的數據類型有關,這使得它看起來是空的。
任何想法? -Sean
什麼版本的sql-server?你是否以XML的形式返回select? http://www.mssqltips.com/sqlservertip/1077/returning-xml-result-sets-with-sql-server/或作爲純文本(varchar)? – 2012-07-11 14:08:10