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 Service Broker 队列从由连接字符串指定的 SQL Server 实例接收。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.

注解

当 SQL Server 连接中出现错误时,SqlDependency 侦听器将重新启动。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.

由于不同用户可以订阅和侦听(使用 SqlCacheDependencySqlCommand)到管理员创建的通知队列,因此,您可以将使用多个线程的应用程序的 SqlDependency 正确地用于表示不同的用户凭据。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)凭据开始调用。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 只调用一次 Start,否则会引发多义性异常。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.

有关其他备注,请参阅 StartFor additional remarks, see Start.

另请参阅

Start(String, String)

启动用于接收依赖项更改通知的侦听器,该侦听器使用指定的 SQL Server Service Broker 队列从由连接字符串指定的 SQL Server 实例接收。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.

有关其他备注,请参阅 StartFor additional remarks, see Start.

另请参阅

适用于