使用数据集和XML

.NET环境中,XML是数据交换的基本格式,In fact ,The xml is the base of the ADO.NET,The DataSet store the data with the Xml inside it,and has many methods  to write and read the xml

For example:

1.System.Data.DataSet的ReadXmlSchema()和WriteXmlSchema(),可以把数据集的结构导出导入为XML模式!

2.可以使用ReadXml()和WriteXml()读取数据集中的数据并且转换为Xml格式!与另一程序交换数据或者制作数据集的本地副本时很有效 !

3.可以把数据集绑定到xml文档上(System.Xml.XmlDocument的实例!),数据集和数据文档是同步的!可以使用ADO.NET 和XML操作来修改它!

ContractedBlock.gifCode

ExpandedBlockStart.gif
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
    
class Program
    {
        
static void Main(string[] args)
        {
            SqlConnection conn 
= new SqlConnection("Server=zhuobin;uid=sa;pwd=zhuobin;database=Northwind");
            
string qry = @"select productname,unitprice from products";
            
try
            { 
              
//Create the DataAdapter
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand 
= new SqlCommand(qry,conn);
                
//Open the connection
                conn.Open();
                
//Create the DataSet
                DataSet ds = new DataSet();
                da.Fill(ds,
"products");
                
//Extract the dataset to the xml
                ds.WriteXml(@"f:\xml\products.xml");
            }
            
catch (SqlException ex)
            {
                Console.WriteLine(
"The error:{0}", ex.Message);
            }
            
finally
            {
                conn.Close();
            }
            Console.ReadLine();
        }
    }
}

转载于:https://www.cnblogs.com/binlyzhuo/archive/2008/11/23/1339429.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值