当前位置: 移动技术网 > IT编程>开发语言>c# > C#从数据库读取数据到DataSet并保存到xml文件的方法

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

2019年07月18日  | 移动技术网IT编程  | 我要评论
本文实例讲述了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#程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网