SQL SERVER-獲取XML資料片段
阿新 • • 發佈:2019-01-05
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
string source = "server=.;uid=sa;pwd=Server2012;database=D:\\DATABASE\\NORTHWND.MDF";
SqlConnection con = new SqlConnection(source);
string sql = "select ContactName,CompanyName from Customers for XML AUTO";
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
XmlReader reader = cmd.ExecuteXmlReader();
reader.Read();
string data;
do
{
data = reader.ReadOuterXml();
if (!string.IsNullOrEmpty(data))
{
Console.WriteLine(data);
}
} while (!string.IsNullOrEmpty(data));
con.Close();
Console.Read();
}
}
}