sqloptions:initialDatabase 標頭元素
未來的 Microsoft SQL Server 版本將移除這項功能。請避免在新的開發工作中使用這項功能,並規劃修改目前使用這項功能的應用程式。
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>
範例
下列是使用 sqloptions:initialDatabase 標頭來搭配 SOAP 要求與回應訊息的範例。
要求
<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>