.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操作来修改它!

Code

 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();
        }
    }
}

相关文章:

  • 2022-03-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-21
  • 2022-12-23
  • 2022-02-23
  • 2021-07-21
猜你喜欢
  • 2022-01-04
  • 2021-08-27
  • 2021-10-22
  • 2022-12-23
  • 2021-05-28
  • 2022-03-09
相关资源
相似解决方案