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 的元素组成。该元素具有一个必需的属性 value。这是一个字符串值。value 属性指定要连接到的初始数据库的名称。该字符串的解释方式如同 USE Transact-SQL 命令。该元素有两个可选属性:optional(布尔值,默认值为 false)和 filename(字符串值,默认值为 null))。optional 属性指定无法连接到数据库时是否应失败。登录:false 表示登录应失败,true 表示登录可以继续。filename 值提供登录时要附加的数据库的文件名。这样,通过附加已分离数据库,用户就可以登录到该数据库。

在一个请求中只能有一个 sqloptions:initialDatabase 选项实例。该选项无法在响应中显示。多个选项实例将会生成错误。

如果接收请求的端点配置了默认数据库,则会生成错误。出于安全考虑,客户端不能对具有默认数据库的端点指定 sqloptions:initialDatabase 标头。

如果使用 SOAP 会话,则必须在与 sqloptions:sqlsession initiate 属性相同的请求中显示此选项。如果在任何带有 sqloptions:sqlSession 标头但不包含 initiate 属性的请求中显示此选项,则将生成错误。

以下是有关 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 帮助