Как определить, какая таблица изменилась в sql зависимости asp.net - PullRequest
0 голосов
/ 22 января 2020

У меня есть две таблицы в моей базе данных. обе эти таблицы объединяются с помощью точки с запятой в команде sql. Я использовал SQL зависимость для уведомления об изменениях. но что я хотел бы знать, что в sqlDep_OnChange, как я могу узнать, какая таблица была изменена первой или второй.

 public void ProductNotification()
    {
        string conStr = ConfigurationManager.ConnectionStrings["sqlConString"].ConnectionString;
        var  sqlCommand = new[] { @"SELECT [ID] from  [dbo].[Products]", @"SELECT [ID] from [dbo].[Customers]"};

        using (SqlConnection con = new SqlConnection(conStr))
        {
            SqlCommand cmd = new SqlCommand(string.Join("; ", sqlCommand), con);

            if (con.State != System.Data.ConnectionState.Open)
            {
                con.Open();
            }
            cmd.Notification = null;
            SqlDependency sqlDep = new SqlDependency(cmd);
            sqlDep.OnChange += sqlDep_OnChange;
            //we must have to execute the command here
            using (SqlDataReader reader = cmd.ExecuteReader())
            {

        }
    }
}


void sqlDep_OnChange(object sender, SqlNotificationEventArgs e)
{

    //which tables has been inserted is the first or second
    if (e.Info == SqlNotificationInfo.Insert)
    {

        SqlDependency sqlDep = sender as SqlDependency;
        sqlDep.OnChange -= sqlDep_OnChange;


        var notificationHub = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
        notificationHub.Clients.All.notify("added");
        //re-register notification
        ProductNotification();
    }
}
...