我有一个用Visual Studio编写的程序。这是代码:
namespace SQL_Connectivity
{
class Program
{
static void Main(string[] args)
{
SqlConnection conn = new SqlConnection("Server=************.database.windows.net,0000;Database=testdb;User ID=testuser;Password= testpassword;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;");
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM District WHERE leaID <= 4 FOR XML PATH('districtEntry'), ROOT('districts')", conn);
SqlDataReader reader = cmd.ExecuteReader();
string path = @"District." + DateTime.Now.ToString("yyyyMMdd") + ".xml";
var writer = new StreamWriter(path);
while(reader.Read())
{
Console.SetOut(writer);
Console.WriteLine(reader.GetString(0));
}
reader.Close();
conn.Close();
}
}
}
假设SqlConnection字符串有效。查询继续查询数据库并以完美格式返回结果。但是,当它通过我的代码时,它会保存在XML文件中,如下所示:
<districts><districtEntry><leaID>01</leaID><name>Alachua</name> <organizationType>LEA</organizationType><streetLine1>620 E University Ave</streetLine1><city>Gainesville</city><stateProvince>FL</stateProvince><postalCode>326015448</postalCode><county>Alachua</county><NCESID_district>1200030</NCESID_district><date_created>2015-06-01T20:38:58.9730000</date_created><date_updated>2015-06-01T20:38:58.9730000</date_updated></districtEntry><districtEntry><leaID>02</leaID><name>Baker</name><organizationType>LEA</organizationType><streetLine1>270 South Blvd E</streetLine1><city>MacClenny</city><stateProvince>FL</stateProvince><postalCode>320632538</postalCode><county>Baker</county><NCESID_district>1200060</NCESID_district><date_created>2015-06-01T20:38:58.9730000</date_created><date_updated>2015-06-01T20:38:58.9730000</date_updated></districtEntry><districtEntry><leaID>03</leaID><name>Bay</name><organizationType>LEA</organizationType><streetLine1>1311 Balboa Ave</streetLine1><city>Panama City</city><stateProvince>FL</st
这一切都以一行结束,而我的代码没有完成XML文件(最后缺少数据)。它似乎总是停在表中最后一行之前的那一行。我也用多个表尝试了这个。
我在问是否有办法
答案 0 :(得分:2)
以下代码解决了原始代码和答案的几个问题:
13.01
13
1300
10.56910569105691
首先,private static void Main(string[] args)
{
using (
var conn =
new SqlConnection(
"Server=************.database.windows.net,0000;Database=testdb;User ID=testuser;Password= testpassword;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;")
)
{
conn.Open();
using (
var cmd =
new SqlCommand(
"SELECT * FROM District WHERE leaID <= 4 FOR XML PATH('districtEntry'), ROOT('districts')",
conn))
{
using (var reader = cmd.ExecuteXmlReader())
{
var doc = XDocument.Load(reader);
string path = @"District." + DateTime.Now.ToString("yyyyMMdd") + ".xml";
using (var writer = new StreamWriter(path))
{
doc.Save(writer);
}
}
}
}
}
,SqlConnection
,SqlCommand
和XmlReader
都需要在StreamWriter
块中,以确保清理它们,即使抛出异常。
其次,using
无法处理所有可能的XML文档。它只能处理那些可以表示为&#34;关系&#34;表。当你想要做的就是解析XML时,构建表和列也是不必要的开销。我改为使用DataSet
。
最后,XDocument
方法使用缩进写出XML。
答案 1 :(得分:1)
你必须使用reader.GetSqlXml来获得结果。 看一眼 What does the SQL Server XML datatype translate to in .NET and how do I convert it to XmlDocument?
答案 2 :(得分:0)
我遇到过一个解决方案&amp;这是我将其更改为的代码:
SqlCommand cmd = new SqlCommand("SELECT * FROM District FOR XML PATH('districtEntry'), ROOT('districts')", conn);
string path = @"District." + DateTime.Now.ToString("yyyyMMdd") + ".xml";
XmlReader reader = cmd.ExecuteXmlReader();
var data = new DataSet();
data.ReadXml(reader);
data.WriteXml(path);
我了解到reader.Read()
正在逐行读取文件并将其写入文件。