次の方法で共有


sqloptions:initialDatabase ヘッダー要素

sqloptions:initialDatabase SOAP ヘッダーにより、クライアントは最初に接続するデータベースを指定できます。次に例を示します。

<SOAP-ENV:Header
  xmlns:SOAP-ENV="https://schemas.xmlsoap.org/soap/envelope/"
  xmlns:sqloptions="https://schemas.microsoft.com/sqlserver/2004/SOAP/Options">
    <sqloptions:initialDatabase 
          SOAP-ENV:mustUnderstand="1"
          value="databaseName" 
          optional="true" | "false" 
          filename="databaseFileName" />
    
</SOAP-ENV:Header>

sqloptions:initialDatabase オプションは SOAP ヘッダーとして指定します。ヘッダーは、sqloptions:initialDatabase という 1 つの要素で構成されています。この要素には 1 つの必須の属性 value があります。これは文字列値です。value 属性には、最初に接続するデータベースの名前を指定します。文字列は、USE Transact-SQL コマンドと同様に解釈されます。要素には、ブール値の optional (既定値は false) と文字列値の filename (既定値は null) の 2 つの省略可能な属性があります。optional 属性には、データベースに接続できなかった場合に失敗するかどうかを指定します。ログインでは、false の場合はログインが失敗することを意味します。true の場合はログインを続行できることを意味します。filename 値には、ログイン時にアタッチするデータベースのファイル名を指定します。これによってデタッチされたデータベースがアタッチされ、ユーザーはそのデータベースにログインできます。

sqloptions:initialDatabase オプションは 1 回の要求で 1 回のみ使用できます。このオプションを応答に使用することはできません。このオプションが複数存在するとエラーが発生します。

要求を受信したエンドポイントに既定のデータベースが構成されている場合、エラーが生成されます。セキュリティ上の理由から、既定のデータベースが構成されたエンドポイントでは、クライアントは sqloptions:initialDatabase ヘッダーを指定できません。

SOAP セッションを使用しているときは、sqloptions:sqlsession initiate 属性と同一の要求でこのオプションを使用する必要があります。initiate 属性が指定されていない sqloptions:sqlSession ヘッダーを含む要求に、このオプションが指定されると、エラーが発生します。

次に、スキーマの sqloptions:initialDatabase オプションに関する部分を示します。

<xs:element name="initialDatabase" form="qualified">
    <xs:annotation>
        <xs:documentation>
            Set initial database on login.
        </xs:documentation>
    </xs:annotation>
    <xs:complexType>
        <xs:attribute use="required" name="value" type="xs:string" form="unqualified">
            <xs:annotation>
                <xs:documentation>
                    The name of the initial database to attach to.
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute
          name="optional"
          default="false"
          type="xs:boolean"
          form="unqualified">
            <xs:annotation>
                <xs:documentation>
                    Whether the initial database is optional or not.
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="filename" type="xs:string" form="unqualified">
            <xs:annotation>
                <xs:documentation>
                    The filename of the database to attach.
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
    </xs:complexType>
</xs:element>

次に、SOAP 要求メッセージと応答メッセージ内で sqloptions:initialDatabase ヘッダーを使用する例を示します。

要求

<SOAP-ENV:Envelope xmlns:SOAP-ENV="https://schemas.xmlsoap.org/soap/envelope/"
                   xmlns:sql="https://schemas.microsoft.com/sqlserver/2004/SOAP"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xmlns:sqlparam="https://schemas.microsoft.com/sqlserver/2004/SOAP/types/SqlParameter"
                   xmlns:sqlsoaptypes="https://schemas.microsoft.com/sqlserver/2004/SOAP/types"
                   xmlns:sqloptions="https://schemas.microsoft.com/sqlserver/2004/SOAP/Options">
  <SOAP-ENV:Header>
    <sqloptions:initialDatabase SOAP-ENV:mustUnderstand="1" 
                                value="dbTestForAttaching" 
                                optional="false" 
                                filename="!DBDATADIR!\dbTestForAttaching.mdf" />
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <sql:sqlbatch>
      <sql:BatchCommands>
        SELECT name FROM sys.databases WHERE database_id=(SELECT dbid FROM master.dbo.sysprocesses WHERE spid=@@spid)
        SELECT TOP 1 object_id FROM sys.objects WHERE object_id=(SELECT TOP 1 object_id FROM dbTestForAttaching.sys.objects)
      </sql:BatchCommands>
    </sql:sqlbatch>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

応答

<SOAP-ENV:Envelope xml:space="preserve" 
                   xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                   xmlns:SOAP-ENV="https://schemas.xmlsoap.org/soap/envelope/" 
                   xmlns:sql="https://schemas.microsoft.com/sqlserver/2004/SOAP" 
                   xmlns:sqlsoaptypes="https://schemas.microsoft.com/sqlserver/2004/SOAP/types" 
                   xmlns:sqlrowcount="https://schemas.microsoft.com/sqlserver/2004/SOAP/types/SqlRowCount" 
                   xmlns:sqlmessage="https://schemas.microsoft.com/sqlserver/2004/SOAP/types/SqlMessage" 
                   xmlns:sqlresultstream="https://schemas.microsoft.com/sqlserver/2004/SOAP/types/SqlResultStream" 
                   xmlns:sqltransaction="https://schemas.microsoft.com/sqlserver/2004/SOAP/types/SqlTransaction" 
                   xmlns:sqltypes="https://schemas.microsoft.com/sqlserver/2004/sqltypes">
  <SOAP-ENV:Body>
    <sql:sqlbatchResponse>
      <sql:sqlbatchResult>
        <sqlresultstream:SqlRowSet xsi:type="sqlsoaptypes:SqlRowSet">
          <diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
            <SqlRowSet1 xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1">
              <row>
                <name>dbTestForAttaching</name>
              </row>
            </SqlRowSet1>
          </diffgr:diffgram>
        </sqlresultstream:SqlRowSet>
        <sqlresultstream:SqlRowCount xsi:type="sqlrowcount:SqlRowCount">
          <sqlrowcount:Count>1</sqlrowcount:Count>
        </sqlresultstream:SqlRowCount>
        <sqlresultstream:SqlRowSet xsi:type="sqlsoaptypes:SqlRowSet">
          <diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
            <SqlRowSet2 xmlns="urn:schemas-microsoft-com:sql:SqlRowSet2">
              <row>
                <object_id>val</object_id>
              </row>
            </SqlRowSet2>
          </diffgr:diffgram>
        </sqlresultstream:SqlRowSet>
        <sqlresultstream:SqlRowCount xsi:type="sqlrowcount:SqlRowCount">
          <sqlrowcount:Count>1</sqlrowcount:Count>
        </sqlresultstream:SqlRowCount>
      </sql:sqlbatchResult>
    </sql:sqlbatchResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

参照

関連項目

SOAP 拡張ヘッダー

ヘルプおよび情報

SQL Server 2005 の参考資料の入手