C#从数据库读取数据到DataSet并保存到xml文件

using System;
using System.Data;
using System.Xml;
using System.Data.SqlClient;
using System.IO;
 
public class TestWriteXML
{
    public static void Main()
    {
        String strFileName = c:/temp/out.xml;
        SqlConnection conn = new SqlConnection(server=localhost;uid=sa;pwd=;database=db);
        String strSql = SELECT name,age FROM people;
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand(strSql, conn);
        // Build the DataSet
        DataSet ds = new DataSet();
        adapter.Fill(ds, employees);
        // Get a FileStream object
        FileStream fs = new FileStream(strFileName, FileMode.OpenOrCreate, FileAccess.Write);
        // Apply the WriteXml method to write an XML document
        ds.WriteXml(fs);
        fs.Close();
    }
}
//该代码片段来自于: http://www.sharejs.com/codes/csharp/7773

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。