次の方法で共有


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

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

  • パラメータ (SqlXmlParameter オブジェクト) を作成する。
  • SqlXmlParameter オブジェクトのプロパティ (Name および Value) に値を割り当てる。

この例では、単純な SQL クエリを実行して、従業員の姓、名、および誕生日を取得します。従業員の姓の値はパラメータとして渡されます。パラメータ (LastName) の指定では、Value プロパティだけを設定します。このクエリでは、パラメータは位置で指定し名前は付けないので、Name プロパティは設定しません。

SqlXmlCommand オブジェクトの CommandType プロパティは、既定では Sql になります。したがって、このプロパティは明示的に設定しません。

ms171740.note(ja-jp,SQL.90).gifメモ :
コードでは、接続文字列に 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;
}
}

アプリケーションをテストするには

この例をテストするには、コンピュータに Microsoft .NET Framework がインストールされている必要があります。

  1. このトピックで提供される C# コード (DocSample.cs) をフォルダに保存します。

  2. コードをコンパイルします。コマンド プロンプトでコードをコンパイルするには、次を使用します。

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

    これにより、実行可能ファイル (DocSample.exe) が作成されます。

  3. コマンド プロンプトで、DocSample.exe を実行します。

コマンド テキストとして 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;
   }
}
ms171740.note(ja-jp,SQL.90).gifメモ :
ExecuteXMLReader メソッドを使用することもできます。このメソッドでは XmlReader オブジェクトが返されます。詳細については、「ExecuteXMLReader メソッドを使用した、SQL クエリの実行」を参照してください。