我需要为插入DB的不同“符号”行设置不同的SqlDependencies。
如何在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
}
答案 0 :(得分:2)
您无法更改事件处理程序的签名。 OnChange
SqlDependency
事件的处理程序将始终接受object
和SqlNotificationEventArgs
,您无法更改此内容。
您可以使用匿名委托来处理该事件。然后,您将能够访问符号参数:
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.
}
}
}