【问题标题】:SqlDependency constant database hitsSqlDependency 常量数据库命中
【发布时间】:2013-01-15 12:22:13
【问题描述】:

我刚刚从微软官方来源http://msdn.microsoft.com/en-us/library/a52dhwx7(v=vs.80).aspx 复制了这个示例,我对此感到困惑。运行应用程序后,即使没有使用该表,它也会不断地命中数据库?我想当该表实际更改时,事件会触发?我不希望它每秒都进行不断的数据库调用,这太糟糕了。

我做错了什么吗?我想我不确定是什么。任何人都有一个很好的例子的链接,最好不要 MSDN。

提前致谢,奥南。

这是 SQL:

return "SELECT [ID],[FromMachine],[FromStore],[FromUser] FROM dbo.Store_Message";

根据要求,所有代码:

    public partial class Form1 : Form
{
    string connectionString = "server=localhost;database=usicoal;uid=admin;password=";

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        GetNames();
    }

    private bool DoesUserHavePermission()
    {
        try
        {
            SqlClientPermission clientPermission = new SqlClientPermission(PermissionState.Unrestricted);
            clientPermission.Demand();
            return true;
        }
        catch
        {
            return false;
        }
    }

    void dep_OnChange(object sender, SqlNotificationEventArgs e)
    {
        if (this.InvokeRequired)
        {
            this.BeginInvoke(new MethodInvoker(GetNames));
        }
        else
        {
            GetNames();
        }
        SqlDependency dep = sender as SqlDependency;
        dep.OnChange -= new OnChangeEventHandler(dep_OnChange);
    }

    private void GetNames()
    {
        if (!DoesUserHavePermission())
            return;

        SqlDependency.Stop(connectionString);
        SqlDependency.Start(connectionString);

        using (SqlConnection cn = new SqlConnection(connectionString))
        {
            using (SqlCommand cmd = cn.CreateCommand())
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "SELECT ID FROM dbo.[BTE_SIMPLE_STORE_MESSAGE]";
                cmd.Notification = null;

                SqlDependency dep = new SqlDependency(cmd);
                dep.OnChange += new OnChangeEventHandler(dep_OnChange);

                cn.Open();

                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                    }
                }
            }
        }
    }
}

【问题讨论】:

    标签: c# sql sqldependency


    【解决方案1】:

    SqlDependency 通常按您的预期工作。如果您的数据库中发生了更改,它会触发事件。如果您的查询有问题,则会发生持续调用。使用完整的两部分表名称(例如 dbo.TableName)很重要。

    MSDN 文档还不错 - 看看这个更新的 article 或这个 one 版本。

    【讨论】:

    • 我看了那篇文章,但是我在一个屏幕上打开了 SQL Management Studio,在另一个屏幕上打开了我的应用程序,它所做的只是循环并使用上述查询不断轮询数据库。我不知道这是否正确,对我来说似乎是错误的?
    • 您是在调试代码还是在 SQL Profiler 中看到查询?如果您在 SQL Profiler 中每秒或更频繁地看到查询 => 那肯定是错误的。可以添加对应的c#代码吗?
    • 添加了我正在使用的代码...在 SQL 分析器下,我一遍又一遍地看到 select 语句,这让我觉得它不起作用。
    • 天哪,它的燃烧数据库是不稳定的 ARGH!刚刚创建了一个新数据库,认为这将是一个长镜头并使用相同的代码并且它 flamin 工作!亲爱的耶稣不知道发生了什么,但正在调查。
    • 兼容级别需要设置在 SQL Server 2008(100)。有什么想法为什么需要设置在这个级别?
    【解决方案2】:

    我发现上面的代码没问题:

    • 右键单击数据库选择属性
    • 选择选项
    • 将兼容级别设置为 SQL Server 2008(100)
    • 好的

    Bang 代码正常工作。

    【讨论】:

      猜你喜欢
      • 2018-12-19
      • 1970-01-01
      • 2014-04-07
      • 1970-01-01
      • 1970-01-01
      • 2013-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多