I am developing an application with SQL Dependency feature.
When i execute the application, subscription was not registered.
and sys.dm_qn_subscriptions
has no entry.
i used SQL profiler to trace the issue and i noticed that subscription was not registered as i did not receive message 1 – Subscription registered
but instead got 3 – subscription fired
with the following Notification message:
<qnev:QNEvent xmlns:qnev="http://schemas.microsoft.com/SQL/Notifications/QueryNotificationProfiler"><qnev:EventText>subscription fired</qnev:EventText><qnev:SubscriptionID>0</qnev:SubscriptionID><qnev:NotificationMsg><qn:QueryNotification xmlns:qn="http://schemas.microsoft.com/SQL/Notifications/QueryNotification" id="0" type="subscribe" source="statement" info="set options" database_id="0" sid="0x01"><qn:Message>2c231f39-60c7-4a73-b575-617a791930f3;d06f0979-8f9b-490e-90ca-6042ab38d68c</qn:Message></qn:QueryNotification></qnev:NotificationMsg><qnev:BrokerDlg>2FAE7196-0E30-E411-8F82-0050569E473E</qnev:BrokerDlg></qnev:QNEvent>
and when i checked it has following information:
Type: Subscription, Source: Statement and Info: set options
and when i checked msdn for the meaning it says The connection options were not set appropriately when the command was submitted.
this is the connection string i am using
<add name="DefaultConnection" connectionString="Data Source=crmserver;Initial Catalog=crmdatabase;Persist Security Info=True;User ID=sa;Password=mypassword" />
and following is the code i am using:
public List<Customer> GetLeadUpdates(SqlDependency dependency)
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
var query = "SELECT id, name, salary FROM [dbo].[CustomerTest]";
connection.Open();
using (var command = new SqlCommand(query, connection))
{
command.Notification = null;
dependency.AddCommandDependency(command);
if (connection.State == ConnectionState.Closed)
connection.Open();
var reader = command.ExecuteReader();
var list = new List<Customer>();
while (reader.Read())
{
var customer = new Customer();
customer.Id = (int) reader["id"];
customer.Name = reader["name"].ToString();
customer.Salary = (int) reader["salary"];
list.Add(customer);
}
return list.OrderBy(p=>p.Id).ToList();
}
}
}
please let me know what i have to do to register the subscription successfully. i have service broker enabled and using "sa" credential in the connection string to connect to db so i suppose i dont need any specific permission to subscribe for notification as sa is the dbo of the database.
Check the connection SET options reported in Profiler to verify the following SET options are effective:
ANSI_NULLS ON
ANSI_PADDING ON
ANSI_WARNINGS ON
CONCAT_NULL_YIELDS_NULL ON
QUOTED_IDENTIFIER ON
NUMERIC_ROUNDABORT OFF
ARITHABORT ON
Note that ARITHABORT
must be set ON
explicitly after the connection if the database compatibility is set to 80 (SQL Server 2000).