Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 为什么我的SqlDependency订阅会出现“open data reader”异常?_.net_Ado.net_Sqldependency - Fatal编程技术网

.net 为什么我的SqlDependency订阅会出现“open data reader”异常?

.net 为什么我的SqlDependency订阅会出现“open data reader”异常?,.net,ado.net,sqldependency,.net,Ado.net,Sqldependency,最后,通过大量功能异常的示例,我成功地接收了SqlDependency对象上的更改消息。这些知识可能会让你对我的代码的不雅或不正确做好准备 我有一个ActiveList:ObservableCollection类,它侦听对其DB表数据源的更改并重新填充自身。我使用以下代码创建并初始化列表和SqlDependency: 建造商: public ActiveList() { PopulateList(); } 填充: private void PopulateList() {

最后,通过大量功能异常的示例,我成功地接收了SqlDependency对象上的更改消息。这些知识可能会让你对我的代码的不雅或不正确做好准备

我有一个ActiveList:ObservableCollection类,它侦听对其DB表数据源的更改并重新填充自身。我使用以下代码创建并初始化列表和SqlDependency:

建造商:

public ActiveList()
{
    PopulateList();
}
填充:

private void PopulateList()
{        Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, ((Action)(() =>
    {
        Clear();
        using (var dbContext = new XTimeDbContext())
        {
            var set = dbContext.Set<T>().ToList();
            this.AddRange(set);
        }
    })));
    SubscribeNotifications();
}
最后,听一听:

private void DependencyOnChange(object sender, SqlNotificationEventArgs sqlNotificationEventArgs)
{
    _trace.TraceInformation("DependencyOnChange called. Reason: '{0}', Source: '{1}', Type: '{2}'.", sqlNotificationEventArgs.Info, sqlNotificationEventArgs.Source,
        sqlNotificationEventArgs.Type);
    //if (!_isPopulating)
    //{
    //    PopulateList();
    //}
    SqlDependency.Stop(_dbContext.Database.Connection.ConnectionString;);
    SubscribeNotifications();
    _trace.TraceInformation("DependencyOnChange completed.");
}
由于大量的小的实验性更改,代码稍微有些混乱,但我的主要问题是,当我运行使用ActiveList的测试应用程序时,我收到了第一个更改通知;我的日志显示DependencyOnChange调用。然后,对SqlDependency.Stop的调用,无论我将其放置在何处,都会生成一个InvalidOperationException,并显示以下消息:

There is already an open DataReader associated with this Command which must be closed first.

我在代码中的任何地方都找不到“悬空”的数据读取器,那么是什么导致了这种情况呢

也许只是一个解决办法。。。但是您是否尝试在连接上将MultipleActiveResultSets设置为true?

您应该调用SqlDependency.StartconnectionString;在开始和SqlDependency.Stop_dbContext.Database.Connection.ConnectionString;时只需一次;;只有一次,当你决定不跟随改变的时候。这些命令为更改事件创建和删除队列

下一行您应该在需要订阅下一个更改时调用

var dependency = new SqlDependency();
dependency.OnChange += DependencyOnChange;
对于exmaple:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace TestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlDependency.Start("server=<MyServer>;database=<MyDB>;User ID=<user>;Password=<pwd>;Integrated Security=false;");
            Console.WriteLine("Started..");
            get_msg();
            Console.ReadLine();
            SqlDependency.Stop("server=<MyServer>;database=<MyDB>;User ID=<user>;Password=<pwd>;Integrated Security=false;");
        }
        private static void get_msg()
        {
            using (SqlConnection con =
                            new SqlConnection("server=<MyServer>;database=<MyDB>;User ID=<user>;Password=<pwd>;Integrated Security=false;"))
            {
                SqlCommand com = new SqlCommand("SELECT MyTableID, SomeText FROM dbo.MyTable ", con);
                SqlDependency dependency = new SqlDependency(com);
                dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
                con.Open();
                com.ExecuteNonQuery();
            }
        }
        static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
        {
            Console.WriteLine("dependency Info = {0}, time: {1}",e.Info, DateTime.Now);
            get_msg();
        }
    }
}

应该记住,SQL依赖关系适用于数据库中的更改不频繁的情况。在代码示例中,下一次更改的订阅是即时的,但最好等待一段时间。

在我的所有连接中都是如此。它从来没有给我带来过它可能造成的一些麻烦。所以我会调用SqlDependency。当我激活我的父模型时开始,它包含一组侦听更改的“表”模型和SqlDependency。当我停用它时停止,而不是单个通知?顺便说一句,这是为了更改查找列表等,所以它们很少出现。@ProfK yes。通知只触发一次,然后您应该订阅下一个更改。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace TestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlDependency.Start("server=<MyServer>;database=<MyDB>;User ID=<user>;Password=<pwd>;Integrated Security=false;");
            Console.WriteLine("Started..");
            get_msg();
            Console.ReadLine();
            SqlDependency.Stop("server=<MyServer>;database=<MyDB>;User ID=<user>;Password=<pwd>;Integrated Security=false;");
        }
        private static void get_msg()
        {
            using (SqlConnection con =
                            new SqlConnection("server=<MyServer>;database=<MyDB>;User ID=<user>;Password=<pwd>;Integrated Security=false;"))
            {
                SqlCommand com = new SqlCommand("SELECT MyTableID, SomeText FROM dbo.MyTable ", con);
                SqlDependency dependency = new SqlDependency(com);
                dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
                con.Open();
                com.ExecuteNonQuery();
            }
        }
        static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
        {
            Console.WriteLine("dependency Info = {0}, time: {1}",e.Info, DateTime.Now);
            get_msg();
        }
    }
}