次の方法で共有


SQL クエリの実行 (SQLXML マネージド クラス)

適用対象:SQL ServerAzure SQL Database

この例では、次のことを行います。

  • パラメーターの作成 (SqlXmlParameter オブジェクト)。

  • SqlXmlParameter オブジェクトのプロパティ (名前と値) に値を割り当てます。

この例では、単純な 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がインストールされている必要があります。

コマンド テキストとして SQL クエリを指定する代わりに、次のコード フラグメントのようなテンプレートを指定して、(同様にテンプレートとして提供されている) アップデートグラムを実行し、顧客レコードを挿入することもできます。 テンプレートとアップデートグラムはファイル内に指定し、ファイルとして実行できます。 詳細については、「 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 の使用

既存のストリームがある場合は、Stream オブジェクトを作成して Execute メソッドを使用する代わりに ExecuteToStream メソッドを使用できます。 前の例のコードは、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 クエリの実行」を参照してください。