Stored procedures Windows应用程序SqlDependency无限调用Onchange
我有一个控制台应用程序,我在其中执行sqldependency。我的问题是,当我将commandType设置为Text时,它工作正常。但若我使用commandType作为StoredProcess,onchange方法将无限调用 请参阅下面的代码:Stored procedures Windows应用程序SqlDependency无限调用Onchange,stored-procedures,infinite,sqldependency,Stored Procedures,Infinite,Sqldependency,我有一个控制台应用程序,我在其中执行sqldependency。我的问题是,当我将commandType设置为Text时,它工作正常。但若我使用commandType作为StoredProcess,onchange方法将无限调用 请参阅下面的代码: static DataSet myDataSet; static SqlConnection connection; static SqlCommand command; stat
static DataSet myDataSet;
static SqlConnection connection;
static SqlCommand command;
static void Main(string[] args)
{
// Remove any existing dependency connection, then create a new one.
string connstr = "Data Source=XYZ;Initial Catalog=Dev;Integrated Security=True";
string ssql = @"[dbo].[SchedulerPendingControlRequestIDFetch]";
CanRequestNotifications();
SqlDependency.Stop(connstr);
SqlDependency.Start(connstr);
if (connection == null)
connection = new SqlConnection(connstr);
if (command == null)
command = new SqlCommand(ssql, connection);
command.CommandType = CommandType.StoredProcedure;
if (myDataSet == null)
myDataSet = new DataSet();
GetAdvtData();
System.Console.ReadKey();
connection.Close();
}
private static bool CanRequestNotifications()
{
SqlClientPermission permission =
new SqlClientPermission(
PermissionState.Unrestricted);
try
{
permission.Demand();
return true;
}
catch (System.Exception)
{
return false;
}
}
private static void GetAdvtData()
{
myDataSet.Clear();
// Ensure the command object does not have a notification object.
command.Notification = null;
// Create and bind the SqlDependency object to the command object.
SqlDependency dependency = new SqlDependency(command,null,100);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
{
adapter.Fill(myDataSet, "ControlRequest");
}
}
private static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
SqlDependency dependency =
(SqlDependency)sender;
dependency.OnChange -= dependency_OnChange;
Console.WriteLine(e.Info.ToString() + e.Source.ToString());
GetAdvtData();
}
我的存储过程是:
IF OBJECT_ID('SchedulerSirasColcoDetailFetch') IS NOT NULL
DROP PROCEDURE SchedulerSirasColcoDetailFetch
Go
PRINT 'Creating stored procedure SchedulerSirasColcoDetailFetch'
Go
CREATE PROCEDURE [dbo].[SchedulerSirasColcoDetailFetch]
AS
BEGIN
SELECT Colco_Code AS 'CountryCode',Connection_String AS 'Url',Resend_Interval AS 'ResendInterval',
Default_Encoding AS 'Encoding' FROM dbo.SirasColcoDetail
END
如果将存储过程中的select语句复制为命令文本,并将commandType设置为文本,则一切正常
你能告诉我是什么问题吗
先谢谢你
Mahesh您应该检查SqlNotificationEventArgs参数的值。仅当您被通知进行数据更改时
您会发现,您不会收到数据更改的通知,但会收到错误设置或错误查询的通知。您的查询和连接设置必须符合中指定的要求。您应该检查SqlNotificationEventArgs参数的值。仅当您被通知进行数据更改时
您会发现,您不会收到数据更改的通知,但会收到错误设置或错误查询的通知。您的查询和连接设置必须符合中指定的要求。在SqlNotificationEventArgs中,我获得的值如下所示:信息:选项来源:语句类型:订阅您的连接/环境选项对于查询通知无效。请阅读我为正确设置提供的链接。在SqlNotificationEventArgs中,我获得的值如下所示:信息:选项来源:语句类型:订阅您的连接/环境选项对于查询通知无效。阅读我提供的正确设置链接。