【问题标题】:C# SqlDependency - Invalid object nameC# SqlDependency - 无效的对象名称
【发布时间】:2017-07-10 19:02:50
【问题描述】:

我想查看 SqlDependency,但在启动时遇到问题。我正在使用下面的代码(来自https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/detecting-changes-with-sqldependency)。

在运行方法 SqlDependency.Start(connString,queue) 时抛出错误“System.Data.SqlClient.SqlException: 'Invalid object name '[core].[intServiceClient_Queue]'.'”。

我在 Sql Server 管理员帐户上连接 SSPI。我确信该对象是服务代理队列并且它存在。

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

【问题讨论】:

  • 检查您的数据库以查看名为 [core].[intServiceClient_Queue] 的对象(表、存储过程、视图等)是否存在。

标签: c# sql tsql service-broker


【解决方案1】:

这是SqlDependency 中的一个错误/缺点:它不支持架构名称。 [core].[intServiceClient_Queue] 被视为对象的名称并转义,从而产生无效的对象名称。将您的队列移动到您用户的默认架构(很可能是dbo),或将默认架构设置为core 并对其他所有内容进行架构限定。

【讨论】:

  • 你是对的。但这很奇怪。在发布问题之前,我在 dbo 模式中创建了同义词,但它也不起作用。
  • @michałkudanowski:连接分析器并查看后台生成的查询。您一定会发现使用同义词(与基础对象名称相反)无法正常工作的内容。
猜你喜欢
  • 2013-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-17
相关资源
最近更新 更多