日期:2014-04-14  浏览次数:20893 次

private 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 语句