欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-24 19:15:40
本文实例讲述了c#从数据库读取数据到dataset并保存到xml文件的方法。分享给大家供大家参考。具体实现方法如下: dataset有一个writexml方法可以直接将数...

本文实例讲述了c#从数据库读取数据到dataset并保存到xml文件的方法。分享给大家供大家参考。具体实现方法如下:

dataset有一个writexml方法可以直接将数据保存到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();
  }
}

希望本文所述对大家的c#程序设计有所帮助。