使用 CommandText 属性执行模板文件

下面的示例演示如何使用 CommandText属性指定由 SQL 或 XPath 查询构成的模板文件。可以将文件名指定为 CommandText 的值,而不必将 SQL 或 XPath 查询指定为该属性的值。在下面的示例中,CommandType 属性指定为 SqlXmlCommandType.TemplateFile

示例应用程序执行下面的模板:

<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
  <sql:query>
    SELECT TOP 2 ContactID, FirstName, LastName 
    FROM   Person.Contact
    FOR XML AUTO
  </sql:query>
</ROOT>

这是 C# 示例应用程序。若要测试该应用程序,请保存模板 (TemplateFile.xml),然后执行该应用程序。

注意注意

在代码中,必须在连接字符串中提供 Microsoft SQL Server 实例的名称。

using System;
using Microsoft.Data.SqlXml;
using System.IO;
class Test
{
      static string ConnString = "Provider=SQLOLEDB;Server=(local);database=AdventureWorks;Integrated Security=SSPI";

      public static int testParams()
      {
         //Stream strm;
         SqlXmlCommand cmd = new SqlXmlCommand(ConnString);
         cmd.CommandType = SqlXmlCommandType.TemplateFile;
         cmd.CommandText = "TemplateFile.xml";
         using (Stream strm = cmd.ExecuteStream()){
            using (StreamReader sr = new StreamReader(strm)){
                Console.WriteLine(sr.ReadToEnd());
            }
         }

         return 0;      
      }
      public static int Main(String[] args)
      {
         testParams();   
         return 0;
      }
   }

测试应用程序

  1. 确保已在计算机上安装了 Microsoft .NET Framework。

  2. 将该示例中提供的 XML 模板 (TemplateFile.xml) 保存在某个文件夹中。

  3. 将在该示例中提供的 C# 代码 (DocSample.cs) 保存到存储架构的相同文件夹中。(如果将文件存储在其他文件夹中,则必须编辑代码并为映射架构指定相应的目录路径。)

  4. 编译代码。若要在命令提示符下编译此代码,请使用:

    csc /reference:Microsoft.Data.SqlXML.dll DocSample.cs
    

    这将创建一个可执行文件 (DocSample.exe)。

  5. 在命令提示符下,执行 DocSample.exe。

如果向模板传递某个参数,参数名称必须以符号 (@) 开头;例如 p.Name="@ContactID",其中 p 是 SqlXmlParameter 对象。

下面是接受一个参数后的已更新模板。

<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
  <sql:header>
     <sql:param name='ContactID'>1</sql:param>  
  </sql:header>
  <sql:query>
    SELECT ContactID, FirstName, LastName
    FROM   Person.Contact
    WHERE  ContactID=@ContactID
    FOR XML AUTO
  </sql:query>
</ROOT>

下面是传递参数所使用的已更新代码,用于执行模板。

   public static int testParams()
   {

      Stream strm;
      SqlXmlParameter p;

      SqlXmlCommand cmd = new SqlXmlCommand(ConnString);
      cmd.CommandType = SqlXmlCommandType.TemplateFile;
      cmd.CommandText = "TemplateFile.xml";
      p = cmd.CreateParameter();
      p.Name="@ContactID";
      p.Value = "1";
      strm = cmd.ExecuteStream();
      StreamReader sw = new StreamReader(strm);
      Console.WriteLine(sw.ReadToEnd());
      return 0;      
   }