2017-06-16 15 views
0

DBに挿入されている異なる「シンボル」行に異なるSqlDependenciesを設定する必要があります。OnChangeEventHandlerにパラメータを追加するにはどうすればいいですか

OnChangeEventHandlerセットアップでSymbolを渡すにはどうすればよいですか?

public void SetDepedencyForSymbol(string symbol) 
{ 
    string cmdText = "SELECT [Symbol] FROM [" + AccountCode + "].[FilledOrders] WHERE [Symbol] = '" + symbol + "'"; 
    using (SqlCommand command = new SqlCommand(cmdText, conn)) 
    { 

     SqlDependency FilledDependency = new SqlDependency(command); 
     FilledDependency.OnChange += new OnChangeEventHandler(OnDependencyForFillsChange); 

     using (SqlDataReader reader = command.ExecuteReader()) 
     { 
      // Process the DataReader. 
     } 
    } 
} 

void OnDependencyForFillsChange(object sender, SqlNotificationEventArgs e) 
{ 
    // Handle the event (for example, invalidate this cache entry). 

    // How can I get value of "symbol" here 
} 
+1

パラメータ化されたクエリを読んで理解し、使用を開始する必要があります。このコードは、SQLインジェクションに対して広く公開されています。手元にある質問については、あなたが何をしようとしているのか分かりません。あなたは宇宙に浮かんでいるコードをいくつか持っていますが、それ以外はメソッドにあります。 –

+0

@SeanLangeメソッドに入れるために質問を編集しました。 OnDependencyForFillsChangeに到達するときに依存関係を設定するために使用された「シンボル」を知りたいです – ManInMoon

答えて

1

イベントハンドラのシグネチャを変更することはできません。 OnChangeイベントのハンドラはSqlDependencyで、常にobjectSqlNotificationEventArgsを受け取り、これを変更することはできません。

匿名の代理人を使用してイベントを処理することもできます。次に、シンボルパラメータにアクセスできるようになります。

public void SetDepedencyForSymbol(string symbol) 
{ 
    string cmdText = "SELECT [Symbol] FROM [" + AccountCode + "].[FilledOrders] WHERE [Symbol] = '" + symbol + "'"; 

    OnChangeEventHandler handler = (sender, args) => 
    { 
     string theSymbol = symbol; 
     // Handle the event (for example, invalidate this cache entry). 

    }; 
    using (SqlCommand command = new SqlCommand(cmdText, conn)) 
    { 

     SqlDependency FilledDependency = new SqlDependency(command); 
     FilledDependency.OnChange += handler; 

     using (SqlDataReader reader = command.ExecuteReader()) 
     { 
      // Process the DataReader. 
     } 
    } 
} 
+0

これは私が欲しかったものです。 – ManInMoon

+0

あなたは歓迎ですが、まだお手伝いしていない場合は、役に立つ回答を投票してください。https://stackoverflow.com/help/privileges/voteup – mm8

関連する問題