Questions about SQLDependency

I am trying setup SQL dependency on local machine using SQL express, and i have following questions

1>Does SQL dependency works with SQLExpress or do i need full SQL Server?

2>On local machine, The dbo user is running under my login account. My login account is "sysadmin" in ./SQLExpress. Im assuming that means it has all the permissions. Do i still need to set all other permissions to run SQLDependency?

3>When i Start dependency by passing queue name, i get the following exception

When using SqlDependency without providing an options value, SqlDependency.Start() must be called prior to execution of a command added to the SqlDependency instance.

 When I dont pass queue name, I dont get exception but .net application doesnt get call back either when SQL table is updated.

(Note that eventually i'm going to call Intialization() method at application start)

Here is my complete code

    public class SqlHelper
    {
        public void Initialization()
        {
            // Create a dependency connection.
            SqlDependency.Start(GetConnectionString(), "SendStatusChangeMessages");
            CanRequestNotifications();
        }

        public void SomeMethod()
        {
            // Assume connection is an open SqlConnection.
            using (SqlConnection con = new SqlConnection(GetConnectionString()))
            {
                con.Open();

                // Create a new SqlCommand object.
                using (SqlCommand command = new SqlCommand(
                    "SELECT BatchStatus from MyTable WHERE ID = 1",
                    con))
                {

                    // 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);

                    Initialization();

                    // Execute the command.
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var message = reader.GetString(0);
                        }
                    }
                }
            }
        }

        // Handler method
        public void OnDependencyChange(object sender,
           SqlNotificationEventArgs e)
        {
            var info = e.Info;
        }

        public void Termination()
        {
            // Release the dependency.
            SqlDependency.Stop(GetConnectionString(), "SendStatusChangeMessages");
        }

        private string GetConnectionString()
        {
            return System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
        }

        private bool CanRequestNotifications()
        {
            SqlClientPermission permission =
                new SqlClientPermission(
                PermissionState.Unrestricted);
            try
            {
                permission.Demand();
                return true;
            }
            catch (System.Exception)
            {
                return false;
            }
        }
    }





  • Edited by lax4u Wednesday, April 30, 2014 4:09 AM
April 30th, 2014 7:02am

Hello,

1.The SqlDependency object represents a query notification dependency between an application and an instance of SQL Server. The Database Engine uses Service Broker to deliver notification messages. Therefore, Service Broker must be active in the database where the application requests the subscription.
Service Broker is fully supported in Express Editions. The only restriction is direct communication between two SQL Server Express servers is not supported. SQL Dependency and query notifications work just fine in SQL Server Express.

Reference:http://msdn.microsoft.com/en-us/system.data.sqlclient.sqldependency
2.The principal that creates the notification must have CREATE SERVICE,CREATE QUEUE,CREATE PROCEDURE in the database where the query runs to successfully create a notification using SqlDependency:
GRANT CREATE PROCEDURE to [User]
GRANT CREATE QUEUE to [User]
GRANT CREATE SERVICE to [User]
Reference:http://msdn.microsoft.com/library/ms188311.aspx

3. Did you create a service which used to route messages, deliver messages to the correct queue?
Reference:http://www.dreamincode.net/forums/topic/156991-using-sqldependency-to-monitor-sql-database-changes/
To understand how SqlDependency works, read on The Mysterious Notification.

Regards,
Fann

Free Windows Admin Tool Kit Click here and download it now
May 1st, 2014 5:48am

Hi Fanny,

Sorry I took so long to get back to you I have been working on other stuff for a couple of weeks and decided to give this another go at home.

The tutorial you referred to was perfect. My main problem turned to be my select:

It was: SELECT * FROM Users

When it should have been: SELECT Id FROM dbo.Users

Never underestimate the powers of simple mistakes I say.

Thanks again.

Dave.

February 21st, 2015 6:43am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics