共用方式為


執行 SQL 查詢 (SQLXML Managed 類別)

適用於: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 物件的 ExecuteXMLReadermethod。 如需詳細資訊,請參閱 使用 ExecuteXMLReader 方法執行 SQL 查詢。