C#從數據庫讀取數據到DataSet並保留到xml文件的辦法。本站提示廣大學習愛好者:(C#從數據庫讀取數據到DataSet並保留到xml文件的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#從數據庫讀取數據到DataSet並保留到xml文件的辦法正文
本文實例講述了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#法式設計有所贊助。