Windows アプリケーションの SqlDepedency が Onchange を無限に呼び出す
-
21-09-2019 - |
質問
sqldependency を実行しているコンソール アプリケーションがあります。私の問題は、commandType を Text に設定すると、正常に動作することです。ただし、commandType を StoredProcedure として使用すると、onchange メソッドが無限に呼び出されます。
以下のコードを参照してください。
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 を Text に設定すると、すべてが正常に動作します。
何が問題なのか教えていただけますか????
よろしくお願いします。
マヘシュ
解決
SqlNotificationEventArgs 引数の値を確認する必要があります。場合のみ タイプ は 変化 そして ソース は データ データ変更を通知した場所。
データの変更については通知されず、間違った設定または間違ったクエリについて通知されることがわかります。クエリと接続の設定は、で指定された要件に準拠する必要があります。 通知用のクエリの作成.
所属していません StackOverflow