C#System.IO.File.WriteAllText;FOR XML查询”;没有将所有数据写入文件
本文关键字:文件 数据 File IO WriteAllText FOR 查询 XML C#System | 更新日期: 2023-09-27 18:29:31
我有以下代码,其中我将查询结果写入xml,然后写入文件。File.WriteAlltext的输出量是否有某种限制?我不明白为什么文件中缺少数据。目前,它似乎不可能超过2kb。谢谢
string query = "SELECT [GUID],Field1,Field2 FROM [Test].[dbo].[Test] WHERE LASTMODIFIED >= '20151230' FOR XML PATH('Test'), ROOT('Tests')";
using (SqlConnection con = new SqlConnection("Data Source=mydatabase ;Initial Catalog=db;User ID=user;Password=pass"))
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
string result = cmd.ExecuteScalar().ToString();
con.Close();
File.WriteAllText(@"E:'Test'file.txt", result);
原来我应该使用ExecuteXmlReader方法读取"FOR XML"查询。否则,数据将在2033被截断,就像我的场景一样。
https://support.microsoft.com/en-us/kb/310378
更新了以con.Open()开头的代码;
con.Open();
var result = cmd.ExecuteScalar().ToString();
con.Close();
XmlDocument doc = new XmlDocument();
doc.LoadXml(result);
XmlTextWriter writer = new XmlTextWriter(@"E:'Test'test.txt", null);
writer.Formatting = Formatting.Indented;
doc.Save(writer);