【问题标题】:ExecuteScalar() not returning full contents of cellExecuteScalar() 不返回单元格的全部内容
【发布时间】:2015-10-27 14:28:17
【问题描述】:

我有一个返回 XML 字符串的存储过程。当我直接从表中复制它时,我得到的一切都完全符合我的预期。

但是,当我尝试使用 C# 运行此存储过程时,ExecuteScalar() 仅返回我期望的 70 行中的 46 行。

这是我正在使用的代码:

using (SqlConnection con = new SqlConnection("Data Source=TEST;Initial Catalog=BMRSK;Integrated Security=True"))
{
    using (SqlCommand buildXML = new SqlCommand("usp_BUILD_RISKCALC_XML", con))
    {
        buildXML.CommandType = CommandType.StoredProcedure;

        con.Open();

        XmlDocument xdoc = new XmlDocument();
        xdoc.LoadXml((string)buildXML.ExecuteScalar());
        xdoc.Save("Test.xml");
    }
}

这是我从该行得到的异常

xdoc.LoadXml((string)buildXML.ExecuteScalar());

“System.Xml.XmlException”类型的未处理异常发生在 System.Xml.dll

附加信息:文件意外结束。这 以下元素未关闭:ARGUMENT、ARGUMENT-LIST、OPERATION、 操作清单,风险计算。第 1 行,位置 2034。

任何想法将不胜感激。谢谢。

【问题讨论】:

  • 能贴出存储过程的代码吗?
  • 开始 xml 标记和结束 xml 标记不匹配。 xml 中有错误。发布 XML。
  • 我已经通过在运行存储过程后右键单击并复制单元格内容来确认它可以工作。现在,我正在尝试使其自动化,但 ExecuteScalar() 只返回前 46 行。应该有 70 个。
  • 返回多少个字符?它与this 匹配吗?有什么理由不改用ExecuteXmlReader

标签: c# sql-server xml


【解决方案1】:

ExecuteScalar 只能读取 2033 个字符。请改用 ExecuteXmlReader。

 private static void BuildXML()
 {
     using (SqlConnection con = new SqlConnection("Data Source=TEST;Initial Catalog=BMRSK;Integrated Security=True"))
     {
         using (SqlCommand buildXML = new SqlCommand("usp_BUILD_RISKCALC_XML", con))
         {
              buildXML.CommandType = CommandType.StoredProcedure;

              con.Open();

              XmlReader xmlReader = buildXML.ExecuteXmlReader();
              XmlDocument xdoc = new XmlDocument();
              xdoc.Load(xmlReader);
              xdoc.Save("Test.xml");
         }
     }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    相关资源
    最近更新 更多