【问题标题】:mssql query, set non xml data sizemssql查询,设置非xml数据大小
【发布时间】:2014-11-10 12:41:30
【问题描述】:

我尝试将一些表(MSSQL)转换为 xml 文件。 结果已被截断为 2.7 MB(距离结束仅 2KB)。

如果我在 vs express 中增加“非 xml 数据”大小和“xml 数据”大小(SQL>执行设置>查询选项...>resutls>grid>“非 xml 数据”和“xml 数据”)然后我得到确切的结果

所以查询工作正常。

唯一的问题,我需要在程序(c#)中而不是在查询窗口中。

我使用 executexmlreader。

有人有理想,有什么错?

        SqlConnection testConnection = new SqlConnection();
        testConnection.ConnectionString = @"Data Source=.\SQLEXPRESS;Integrated Security=True;Connect Timeout=30;";
        testConnection.Open();

        TestCommand = new SqlCommand("exec prAdvListToXML", testConnection);

        XmlReader TestXmlReader = TestCommand.ExecuteXmlReader();
        XmlWriter TestFileWriter = XmlWriter.Create(@"C:\temp\output.xml");
        TestFileWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-16'");

        TestFileWriter.WriteNode(TestXmlReader, true);

【问题讨论】:

    标签: c# sql-server xml


    【解决方案1】:

    我怀疑您的问题根本与 XML 无关(当然,Management Studio 的行为是一个红鲱鱼)。您不会在任何地方关闭您的编写器,也不会确保所有数据都已处理。尝试重写您的代码,以便正确处理资源:

    using (var testConnection = new SqlConnection()) {
        testConnection.ConnectionString = @"Data Source=.\SQLEXPRESS;Integrated Security=True;Connect Timeout=30;";
        testConnection.Open();
        using (var testCommand = new SqlCommand("exec prAdvListToXML", testConnection))
        using (XmlReader testXmlReader = testCommand.ExecuteXmlReader())
        using (XmlWriter testFileWriter = XmlWriter.Create(@"C:\temp\output.xml")) {
            testFileWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-16'");
            testFileWriter.WriteNode(testXmlReader, true);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      • 1970-01-01
      • 2012-10-08
      • 2019-07-25
      相关资源
      最近更新 更多