Can SQL Server push message to a program which listens to SQL Server?
For example:
There is a program A, listening for SQL Server. SQL Server will view a table named B, when B has some data, SQL Server will get the data and push it to A.
Can SQL Server push message to a program which listens to SQL Server?
For example:
There is a program A, listening for SQL Server. SQL Server will view a table named B, when B has some data, SQL Server will get the data and push it to A.
Yes, it's possible, see How to run a program from SQL?.
But, as that post states, there are a lot of reasons not to do so. SQL Server was written to be queried, so it will be a lot more efficient answering queries than pushing them.
Are you looking for some kind of SqlServerNotifications | MSDN? For me that did not work because it requers some Configuration that we were not able to implement ( security reasons ) ... so i implemented my own notification layer with my own TCP network wrapper. When one client updates the database, it sends to all other clients a message with ID and table name, and the Client will update the entry by it self. Its not easy to implement and requers a lot of desgin.
You can use SqlDependecy (class Details) to detect changes in tables/views. This does require Enabling Query Notifications.
void Initialization()
{
// Create a dependency connection.
SqlDependency.Start(connectionString, queueName);
}
void SomeMethod()
{
// Assume connection is an open SqlConnection.
// Create a new SqlCommand object.
using (SqlCommand command=new SqlCommand(
"SELECT ShipperID, CompanyName, Phone FROM dbo.Shippers",
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);
}
To use SqlDependency, Service Broker must be enabled for the SQL Server database being used, and users must have permissions to receive notifications. Service Broker objects, such as the notification queue, are predefined.