当我在不使用SQLD依赖关系的情况下更新表数据时,如何使用商店过程在C#方法上获取呼叫触发器



当我更新表数据或在[dbo]中删除sql newquery'delete'delete top(1)时,我想调用C#方法。使用商店过程调用C#方法。我在SQL依赖性上执行它,但我不需要此操作,我使用商店过程执行它。您可以看到我的SQLDEPPENECEY代码。但是我想要另一种使用商店过程调用此方法的方式。

public class NotificationEvent
{
    private delegate void RateChangeNotification(DataTable table);
    private SqlDependency dependency;
    string ConnectionString = @"Data Source=.;Initial Catalog=Message;Integrated Security=True";
    string UserName = Environment.UserName;
    public void RegisterForNotification()
    {
        var connectionString = ConnectionString;
        using (var connection = new SqlConnection(connectionString))
        {
            connection.Open();
            var queryString = "SELECT [ID] FROM [dbo].[Leaves]";
            using (var oCommand = new SqlCommand(queryString, connection))
            {
                // Starting the listener infrastructure...
                SqlDependency.Start(connectionString);
                var oDependency = new SqlDependency(oCommand);
                oDependency.OnChange += OnNotificationChange;
                // NOTE: You have to execute the command, or the notification will never fire.
                oCommand.ExecuteReader();
            }
        }
    }

    private void OnNotificationChange(object sender, SqlNotificationEventArgs e)
    {
        Console.WriteLine("Notification Info: " + e.Info);
        //Re-register the SqlDependency. 
        //var oDependency = new SqlDependency();
        //oDependency.OnChange += OnNotificationChange;
        RegisterForNotification();
    }
}

@umar asif如果您遇到的问题完全涉及db-to-db通信,我会建议在sql server中推荐一个称为"合并复制"的概念(使用Publisher-subscriber(S))DBS之间的设计:

https://learn.microsoft.com/en-us/sql/relational-databases/replication/merge/merge/merge-replication?view=sql-sql-server-2017

否则,如果您的问题仅通过呼叫C#方法需要解决方案,请参考:

如何在存储过程中调用C#函数

最新更新