执行 SQL 查询(SQLXML 托管类)

适用于:SQL ServerAzure SQL 数据库

此示例演示:

  • (SqlXmlParameter 对象) 创建参数。

  • 为属性赋值 (SqlXmlParameter 对象的 Name 和 Value) 。

在本示例中,将执行一个简单的 SQL 查询,以检索其姓氏值作为参数传递的雇员的名字、姓氏和出生日期。 在指定 参数 (LastName) 时,仅设置 Value 属性。 未设置 Name 属性,因为在此查询中,参数是位置参数,不需要名称。

SqlXmlCommand 对象的 CommandType 属性默认为 Sql。 因此,不显式设置此属性。

注意

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

以下是 C# 代码:

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;  
         SqlXmlParameter p;  
         SqlXmlCommand cmd = new SqlXmlCommand(ConnString);        
         cmd.CommandText = "SELECT FirstName, LastName FROM Person.Contact WHERE LastName=? For XML Auto";  
         p = cmd.CreateParameter();  
         p.Value = "Achong";  
         string strResult;  
         try   
         {  
            strm = cmd.ExecuteStream();  
            strm.Position = 0;  
            using(StreamReader sr = new StreamReader(strm))  
            {  
               Console.WriteLine(sr.ReadToEnd());  
            }  
         }  
         catch (SqlXmlException e)  
         {  
            //in case of an error, this prints error returned.  
            e.ErrorStream.Position=0;  
            strResult=new StreamReader(e.ErrorStream).ReadToEnd();  
            System.Console.WriteLine(strResult);  
         }  
  
         return 0;  
   }  
public static int Main(String[] args)  
{  
    testParams();  
    return 0;  
}  
}  

测试应用程序

  1. 将本主题中提供的 C# 代码 (DocSample.cs) 保存在某个文件夹中。

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

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

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

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

若要测试此示例,必须在计算机上安装 Microsoft .NET Framework。

您可以指定一个模板(如下面的代码段所示),该模板执行 updategram(也是一个模板)来插入客户记录,而不是将 SQL 查询指定为命令文本。 您可以在文件中指定模板和 updategram,然后执行文件。 有关详细信息,请参阅 使用 CommandText 属性执行模板文件

SqlXmlCommand cmd = new SqlXmlCommand("Provider=SQLOLEDB;Data Source=SqlServerName;Initial Catalog=Database; Integrated Security=SSPI;");  
Stream stm;  
cmd.CommandType = SqlXmlCommandType.UpdateGram;  
cmd.CommandText = "<ROOT xmlns:sql='urn:schemas-microsoft-com:xml-sql' xmlns:updg='urn:schemas-microsoft-com:xml-updategram'>" +  
      "<updg:sync>" +  
       "<updg:before/>" +  
       "<updg:after>" +  
         "<Customer CustomerID='aaaaa' CustomerName='Some Name' CustomerTitle='SomeTitle' />" +  
       "</updg:after>" +  
       "</updg:sync>" +  
       "</ROOT>";  
  
stm = cmd.ExecuteStream();  
stm = null;  
cmd = null;  

使用 ExecuteToStream

如果已有流,则可以使用 ExecuteToStream 方法,而不是创建 Stream 对象并使用 Execute 方法。 此处修改了上述示例中的代码,以使用 ExecuteToStream 方法:

using System;  
using Microsoft.Data.SqlXml;  
using System.IO;  
class Test  
{  
   static string ConnString = "Provider=SQLOLEDB;Server=SqlServerName;database=AdventureWorks;Integrated Security=SSPI;";  
   public static int testParams()  
   {  
      SqlXmlParameter p;  
      MemoryStream ms = new MemoryStream();  
      StreamReader sr = new StreamReader(ms);  
      ms.Position = 0;  
      SqlXmlCommand cmd = new SqlXmlCommand(ConnString);  
      cmd.CommandText = "select FirstName, LastName from Person.Contact where LastName = ? For XML Auto";  
      p = cmd.CreateParameter();  
      p.Value = "Achong";  
      cmd.ExecuteToStream(ms);  
      ms.Position = 0;  
      Console.WriteLine(sr.ReadToEnd());  
      return 0;        
   }  
   public static int Main(String[] args)  
   {  
      testParams();     
      return 0;  
   }  
}  

注意

还可以使用返回 XmlReader 对象的 ExecuteXMLReader 方法。 有关详细信息,请参阅 使用 ExecuteXMLReader 方法执行 SQL 查询