我正在尝试为当前项目实施查询通知。我正在使用SqlDependency
. 当我直接在 Ado.net 命令中为查询执行内联 SQL 时,我能够使代码正常工作,但是,当我将相同的确切查询放入存储过程中时,查询通知似乎不起作用。它在事件 argsSqlNotificationEventArgs
的参数中返回 Query(8) 的值。e.Info
它还继续发射约。每 30 多秒。
Public Sub GetCust(ByVal CustomerId As String)
Dim oConn As SqlConnection
Dim connectionString As string
connectionString = GetConnectionString()
SqlDependency.Stop(connectionString)
SqlDependency.Start(connectionString)
oConn = New SqlConnection(connectionString)
'This is the code for the stored procedure path
'Dim oCommand As New SqlCommand("SelectCustomerInfo", oConn)
'oCommand.CommandType = CommandType.StoredProcedure
'oCommand.Parameters.AddWithValue("@CustomerId", CustomerId)
'This is the code for using inline SQL
Dim strSQL As String
strSQL = "select [CustomerId],[name], [age],[gender],[favorite_food] from dbo.custpreferences where customerid = '" & CustomerId & "'"
Dim oCommand As New SqlCommand(strSQL, oConn)
oCommand.CommandType = CommandType.Text
oCommand.Notification = Nothing
Dim dep As SqlDependency = New SqlDependency(oCommand)
AddHandler dep.OnChange, AddressOf cust_onchange
oConn.Open()
Dim dataAdapter As SqlDataAdapter = New SqlDataAdapter(oCommand)
dataAdapter.Fill(dsCustomers)
If (Not dataAdapter Is Nothing) Then dataAdapter.Dispose()
If Not oConn Is Nothing Then oConn.Dispose()
End Sub
Private Sub cust_onchange(ByVal sender As System.Object, ByVal e As System.Data.SqlClient.SqlNotificationEventArgs)
GetCustInfo(mCustomer.strCustomerID)
Dim dep As SqlDependency = DirectCast(sender, SqlDependency)
RemoveHandler dep.OnChange, AddressOf cust_onchange
End Sub
这是存储过程:
SET ANSI_NULLS ON
GO
SET ANSI_PADDING ON
GO
SET ANSI_WARNINGS ON
GO
SET CONCAT_NULL_YIELDS_NULL ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET NUMERIC_ROUNDABORT OFF
GO
SET ARITHABORT ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[SelectCustInfo]
@CustomerID VARCHAR(6)
AS
SELECT
[CustomerId], [name], [age], [gender], [favorite_food]
FROM
dbo.CustInfo (NOLOCK)
WHERE
(CustomerId = @CustomerID)
任何想法为什么这适用于内联 SQL 与使用存储过程?