为什么我使用SqlDependency

时间:2016-02-23 11:16:19

标签: c# sql-server sqldependency

我的数据库管理员昨天必须更改一些权限/所有权。以前我的SqlDependency代码运行良好。

但现在我认为这是一个凭据问题(我相信这是因为我经过长时间的网络搜索后看到了一个小注释)。

我使用此连接字符串成功连接到MS SQL:

        connectionString = @"user id=billybob;" +
                                    @"password=bigsecret;" +
                                   "server=11.2.133.8;" +
                                   "Trusted_Connection=no;" +
                                   "database=MYDB; " +
                                   "MultipleActiveResultSets=true;" +
                                   "connection timeout=30";

然后我尝试用:

启动SqlDependency
SqlDependency.Start(connectionString, null);

但是我收到了这个错误:

  

System.Data.SqlClient.SqlException未处理HResult = -2146232060   Message = Subquery返回的值超过1。这是不允许的   当子查询跟随=,!=,<,< =,>,> =或子查询是   用作表达。声明已经终止。

可能是什么问题?

基兰尼的富勒追踪:

  

子查询返回的值超过1。这是不允许的   子查询跟随=,!=,<,< =,>,> =或当子查询用作   一种表达。该语句已终止。在   System.Data.SqlClient.SqlConnection.OnError(SqlException异常,   Boolean breakConnection,Action 1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource 1   completion,String methodName,Boolean sendToPipe,Int32 timeout,   布尔asyncWrite)at   System.Data.SqlClient.SqlCommand.ExecuteNonQuery()at   SqlDependencyProcessDispatcher.SqlConnectionContainer.CreateQueueAndService(布尔   重启)   SqlDependencyProcessDispatcher.SqlConnectionContainer..ctor(SqlConnectionContainerHashHelper   hashHelper,String appDomainKey,Boolean useDefaults)at   SqlDependencyProcessDispatcher.Start(String connectionString,String&   服务器,DbConnectionPoolIdentity&身份,字符串&用户,字符串&   数据库,字符串& queueService,String appDomainKey,   SqlDependencyPerAppDomainDispatcher dispatcher,Boolean&   errorOccurred,Boolean& appDomainStart,Boolean useDefaults)at   SqlDependencyProcessDispatcher.StartWithDefault(字符串   connectionString,String&服务器,DbConnectionPoolIdentity&身份,   字符串和放大器;用户,字符串&数据库,字符串& service,String appDomainKey,   SqlDependencyPerAppDomainDispatcher dispatcher,Boolean&   errorOccurred,Boolean& appDomainStart)at   System.Data.SqlClient.SqlDependency.Start(String connectionString,   字符串队列,布尔值useDefaults)

这是代码:

在创建查询之前发生错误注释错误!

public void InitialiseDependency(Action onDependencyMethod)
{
    this.onDependencyMethod = onDependencyMethod;
    SqlDependency.Start(connectionString, null); //<<<< ERROR ON THIS LINE

    using (SqlCommand command = new SqlCommand(
            "SELECT [Symbol] FROM [dbo].[tbls] WHERE [Status] = 'NEW'",
            conn))
    {

        SqlDependency dependency = new SqlDependency(command);


        dependency.OnChange += new OnChangeEventHandler(OnDependencyChange);

        using (SqlDataReader reader = command.ExecuteReader())
        {
            // Process the DataReader.
        }
    }
}

1 个答案:

答案 0 :(得分:0)

最终我们删除并重新创建了用户“billybob”。一旦我们这样做,错误就消失了......

相关问题