码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2015-05-18 07:46:21      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

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

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

标签:

原文地址:http://www.cnblogs.com/wordgao/p/4510962.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!