从SQL Server中获取XML

80酷酷网    80kuku.com

  server|xmlprivate string GetFORXML(string sFile, string sSQL)
  {
   // Create and open the connection to Northwind
   DataSet oDs = new DataSet();
   using(SqlConnection oCn = new SqlConnection(ConnectionString))
   {
    oCn.Open();
   
    // Create the SQL command to execute
    SqlCommand oCmd = new SqlCommand(sSQL, oCn);

    System.Diagnostics.Debug.WriteLine(sSQL);
   
    // Execute the SQL statement and return the data to an XmlReader
    XmlReader oXml = oCmd.ExecuteXmlReader();

    // Read the Schema of the XML
    oDs.ReadXmlSchema(oXml);

    // Read the XML fragment (which needs a schema)
    oDs.ReadXml(oXml, XmlReadMode.Fragment);
   }
   oDs.DataSetName = "root";
   
   // Write the XML out to a file
   oDs.WriteXml(sPath + sFile);
   
   return "Wrote XML to file " + sFile;
  }

方法中的 sSQL 是含有 FOR XML 的SQL 语句




分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: