分享

根据数据库生成xml二法

 悟静 2011-04-10
SqlConnection conn = new SqlConnection();
      conn.ConnectionString = "Server=127.0.0.1;User ID=sa;Password=fdahgdrethj31313210212121;Database=northwind;Persist Security Info=True";
      conn.Open();
      SqlDataAdapter da = new SqlDataAdapter("select * from 表", conn);
      SqlCommandBuilder thisBulder = new SqlCommandBuilder(da);
      DataSet ds = new DataSet();
      da.Fill(ds);
      ds.WriteXml(@"C:\temp.xml");
==============================================================================
private void WriteXmlToFile(DataSet thisDataSet) {
    if (thisDataSet == null) { return; }
    // Create a file name to write to.
    string filename = "myXmlDoc.xml";
    // Create the FileStream to write with.
    System.IO.FileStream myFileStream = new System.IO.FileStream
       (filename, System.IO.FileMode.Create);
    // Create an XmlTextWriter with the fileStream.
    System.Xml.XmlTextWriter myXmlWriter =
       new System.Xml.XmlTextWriter(myFileStream, System.Text.Encoding.Unicode);
    // Write to the file with the WriteXml method.
    thisDataSet.WriteXml(myXmlWriter);  
    myXmlWriter.Close();
 }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约