SQL Database Change Detection

Consider the following example:

INSERT INTO [Table] (column1)
SELECT value1

If I had to execute this command in SSMS, regarding a C # forms application, what do I need to do to recognize this event? Something simple, like an application that displays MessageBoxwhen this event occurs. I just can't work with this or find useful data. I tried to use SqlDependency, but I was out of luck. If this is the way I need to go down, can someone help me with understanding the concept a little better?

+4
source share
1 answer

, , , SQL Dependency. ?

"/" , .

.

  • SqlNotificationRequest, , .

  • SqlDependency, , SQL Server, . SQL Server .NET Framework SQL Server.

  • , -, ASP.NET 2.0 , SqlCacheDependency.

, " SqlDependency SqlCommand, , , ".

void Initialization()
{
    // Create a dependency connection.
    SqlDependency.Start(connectionString, queueName);
}

void SomeMethod()
{
    // Assume connection is an open SqlConnection.
    // Create a new SqlCommand object which directly references (no synonyms) the data you want to check for changes.
    using (SqlCommand command=new SqlCommand("SELECT value1 FROM [Table]", connection))
    {
        // Create a dependency and associate it with the SqlCommand.
        SqlDependency dependency=new SqlDependency(command);
        // Maintain the refence in a class member.

        // Subscribe to the SqlDependency event.
        dependency.OnChange+=new OnChangeEventHandler(OnDependencyChange);

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

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

void Termination()
{
    // Release the dependency.
    SqlDependency.Stop(connectionString, queueName);
}
+7

All Articles