本文介绍了找不到对象“queuename”在sqldependency?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I got following error in SQLDependency code
Cannot find the object "QueueName" because it does not exist or you do not have permissions.
I already gave permission thru following command
"GRANT SUBSCRIBE QUERY NOTIFICATIONS TO [SqlUser];"
Any other permission is required for SQLDependency ?
What changes required to resolved this issue .
My code is
string strConnectionString = @"Data Source=servername;Initial Catalog=dbname;Integrated Security=True;MultipleActiveResultSets=True;";
// To prevent errors, when database connection is lost, always stop SqlDependency subscription, before starting one.
using (var connection = new SqlConnection(strConnectionString))
{
// Define query.
string sqlQuery = "SELECT empname from emp ";
// Open connection to database.
connection.Open();
using (SqlCommand command = new SqlCommand(sqlQuery, connection))
{
SqlDependency.Stop(strConnectionString, "QueueName");
SqlDependency.Start(strConnectionString, "QueueName");
command.CommandType = System.Data.CommandType.Text;
// Make sure the command object does not already have a notification object associated with it.
command.Notification = null;
// Hookup sqldependency eventlistener (re-register for change events).
var dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
// Open connection to database.
if (connection.State != System.Data.ConnectionState.Open)
{
connection.Open();
}
// Get the data from the database and convert to a list .
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
var data = new List<Process>();
while (reader.Read())
{
var da = new Process
{
EmpName = reader.GetString(0),
ToMobile = reader.GetString(1)
};
data.Add(da);
}
_data = data;
}
}
connection.Close();
}
}
return _data;
我尝试了什么:
What I have tried:
I got following error in SQLDependency code
Cannot find the object "QueueName" because it does not exist or you do not have permissions.
I already gave permission thru following command
"GRANT SUBSCRIBE QUERY NOTIFICATIONS TO [SqlUser];"
Any other permission is required for SQLDependency ?
What changes required to resolved this issue .
My code is
string strConnectionString = @"Data Source=servername;Initial Catalog=dbname;Integrated Security=True;MultipleActiveResultSets=True;";
// To prevent errors, when database connection is lost, always stop SqlDependency subscription, before starting one.
using (var connection = new SqlConnection(strConnectionString))
{
// Define query.
string sqlQuery = "SELECT empname from emp ";
// Open connection to database.
connection.Open();
using (SqlCommand command = new SqlCommand(sqlQuery, connection))
{
SqlDependency.Stop(strConnectionString, "QueueName");
SqlDependency.Start(strConnectionString, "QueueName");
command.CommandType = System.Data.CommandType.Text;
// Make sure the command object does not already have a notification object associated with it.
command.Notification = null;
// Hookup sqldependency eventlistener (re-register for change events).
var dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
// Open connection to database.
if (connection.State != System.Data.ConnectionState.Open)
{
connection.Open();
}
// Get the data from the database and convert to a list .
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
var data = new List<Process>();
while (reader.Read())
{
var da = new Process
{
EmpName = reader.GetString(0),
ToMobile = reader.GetString(1)
};
data.Add(da);
}
_data = data;
}
}
connection.Close();
}
}
return _data;
推荐答案
这篇关于找不到对象“queuename”在sqldependency?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!