SqlDependency.Start 方法

定義

啟動接聽程式,以接收相依性變更告知。Starts the listener for receiving dependency change notifications.

多載

Start(String)

啟動接聽程式,從連接字串所指定的 SQL Server 執行個體接收相依性變更告知。Starts the listener for receiving dependency change notifications from the instance of SQL Server specified by the connection string.

Start(String, String)

啟動接聽程式,從連接字串所指定的 SQL Server 執行個體並使用指定的 SQL Server Service Broker 佇列,接收相依性變更告知。Starts the listener for receiving dependency change notifications from the instance of SQL Server specified by the connection string using the specified SQL Server Service Broker queue.

備註

SqlDependency SQL Server 連接中發生錯誤時, 接聽程式將會重新開機。The SqlDependency listener will restart when an error occurs in the SQL Server connection.

可以對Start方法進行多個呼叫, 但受限於下列限制:Multiple calls to the Start method can be made, subject to the following restrictions:

  • 具有相同參數的多個呼叫 (在呼叫執行緒中的相同連接字串和 Windows 認證) 是有效的。Multiple calls with identical parameters (the same connection string and Windows credentials in the calling thread) are valid.

  • 具有不同連接字串的多個呼叫有效, 只要:Multiple calls with different connection strings are valid as long as:

    • 每個連接字串會指定不同的資料庫, 或Each connection string specifies a different database, or

    • 每個連接字串會指定不同的使用者, 或Each connection string specifies a different user, or

    • 呼叫來自不同的應用程式域。The calls come from different application domains.

您可以SqlDependency為使用多個執行緒的應用程式提供正確的工作來代表不同的使用者認證, 而不需要將 dbo 角色授與群組, 因為不同的使用者可以SqlCommand訂閱和接聽 (使用SqlCacheDependency或)到系統管理員所建立的通知佇列。You can make the SqlDependency work correctly for applications that use multiple threads to represent different user credentials without giving the dbo role to the group, because different users can subscribe and listen (using SqlCacheDependency or SqlCommand) to a notification queue created by an administrator. 當相關的應用程式域啟動時, 請使用具有初始化服務/佇列之許可權的使用者 (Windows) 認證 (資料庫的建立佇列和建立服務許可權) 來呼叫 Start。When the relevant application domain starts, call Start with the (Windows) credentials of a user that has permission to initialize a service/queue (the CREATE QUEUE and CREATE SERVICE permissions for the database). 請確定每個 AppDomain 只會呼叫一次啟動, 否則會引發不明確的例外狀況。Ensure that Start is only called once per AppDomain, otherwise an ambiguity exception is raised. 使用者執行緒必須擁有訂閱通知的許可權 (資料庫的訂閱查詢通知許可權)。The user thread must have permission to subscribe to the notification (the SUBSCRIBE QUERY NOTIFICATIONS permission for the database). SqlDependency會將非系統管理員使用者的訂閱要求與系統管理員所建立的服務/佇列產生關聯。SqlDependency will associate the subscription request of a non-administrator user to the service/queue created by the administrator.

Start(String)

啟動接聽程式,從連接字串所指定的 SQL Server 執行個體接收相依性變更告知。Starts the listener for receiving dependency change notifications from the instance of SQL Server specified by the connection string.

public:
 static bool Start(System::String ^ connectionString);
public static bool Start (string connectionString);
static member Start : string -> bool
Public Shared Function Start (connectionString As String) As Boolean

參數

connectionString
String

連接字串,其指定從中取得變更告知的 SQL Server 執行個體。The connection string for the instance of SQL Server from which to obtain change notifications.

傳回

如果接聽程式成功初始化,則為 true;如果相容的接聽程式已存在,則為 falsetrue if the listener initialized successfully; false if a compatible listener already exists.

例外狀況

connectionString 參數為 NULL。The connectionString parameter is NULL.

connectionString 參數和這個方法的前一個呼叫相同,但參數不同。The connectionString parameter is the same as a previous call to this method, but the parameters are different.

方法從 CLR 中呼叫。The method was called from within the CLR.

呼叫端沒有必要的 SqlClientPermission 程式碼存取安全性 (CAS) 使用權限。The caller does not have the required SqlClientPermission code access security (CAS) permission.

後續的方法呼叫使用對等 connectionString 參數,但使用者不同或使用者未預設為相同結構描述。A subsequent call to the method has been made with an equivalent connectionString parameter with a different user, or a user that does not default to the same schema.

此外,也會發生任何基礎 SqlClient 例外狀況。Also, any underlying SqlClient exceptions.

備註

這個方法會啟動AppDomain的接聽程式, 以便從connectionString參數所指定的 SQL Server 實例接收相依性通知。This method starts the listener for the AppDomain for receiving dependency notifications from the instance of SQL Server specified by the connectionString parameter. 針對多部伺服器, 可以使用不同的連接字串來呼叫這個方法一次以上。This method may be called more than once with different connection strings for multiple servers.

如需其他備註, Start請參閱。For additional remarks, see Start.

另請參閱

Start(String, String)

啟動接聽程式,從連接字串所指定的 SQL Server 執行個體並使用指定的 SQL Server Service Broker 佇列,接收相依性變更告知。Starts the listener for receiving dependency change notifications from the instance of SQL Server specified by the connection string using the specified SQL Server Service Broker queue.

public:
 static bool Start(System::String ^ connectionString, System::String ^ queue);
public static bool Start (string connectionString, string queue);
static member Start : string * string -> bool
Public Shared Function Start (connectionString As String, queue As String) As Boolean

參數

connectionString
String

連接字串,其指定從中取得變更告知的 SQL Server 執行個體。The connection string for the instance of SQL Server from which to obtain change notifications.

queue
String

所要使用的現有 SQL Server Service Broker 佇列。An existing SQL Server Service Broker queue to be used. 如果是 null,就會使用預設的佇列。If null, the default queue is used.

傳回

如果接聽程式成功初始化,則為 true;如果相容的接聽程式已存在,則為 falsetrue if the listener initialized successfully; false if a compatible listener already exists.

例外狀況

connectionString 參數為 NULL。The connectionString parameter is NULL.

connectionString 參數和這個方法的前一個呼叫相同,但參數不同。The connectionString parameter is the same as a previous call to this method, but the parameters are different.

方法從 CLR 中呼叫。The method was called from within the CLR.

呼叫端沒有必要的 SqlClientPermission 程式碼存取安全性 (CAS) 使用權限。The caller does not have the required SqlClientPermission code access security (CAS) permission.

後續的方法呼叫使用對等 connectionString 參數,但使用者不同或使用者未預設為相同結構描述。A subsequent call to the method has been made with an equivalent connectionString parameter but a different user, or a user that does not default to the same schema.

此外,也會發生任何基礎 SqlClient 例外狀況。Also, any underlying SqlClient exceptions.

備註

這個方法會啟動AppDomain的接聽程式, 以便從connectionString參數所指定的 SQL Server 實例接收相依性通知。This method starts the listener for the AppDomain for receiving dependency notifications from the instance of SQL Server specified by the connectionString parameter. 針對多部伺服器, 可以使用不同的連接字串來呼叫這個方法一次以上。This method may be called more than once with different connection strings for multiple servers.

如果未指定任何佇列名稱, SqlDependency則會在用於整個進程的伺服器中建立暫存佇列和服務, 即使該進程牽涉到多AppDomain個。If no queue name is specified, SqlDependency creates a temporary queue and service in the server that is used for the entire process, even if the process involves more than one AppDomain. 佇列和服務會在應用程式關閉時自動移除。The queue and service are automatically removed upon application shutdown.

如需其他備註, Start請參閱。For additional remarks, see Start.

另請參閱

適用於