简体   繁体   English

SQL缓存依赖项不适用于存储过程

[英]SQL Cache Dependency not working with Stored Procedure

I can't get SqlCacheDependency to work with a simple stored proc (SQL Server 2008): 我无法使SqlCacheDependency与简单的存储过程(SQL Server 2008)一起使用:

create proc dbo.spGetPeteTest
as

set  ANSI_NULLS ON
set ANSI_PADDING ON
set ANSI_WARNINGS ON
set CONCAT_NULL_YIELDS_NULL ON
set QUOTED_IDENTIFIER ON
set NUMERIC_ROUNDABORT OFF
set ARITHABORT ON

select Id, Artist, Album
from dbo.PeteTest

And here's my ASP.NET code (3.5 framework): 这是我的ASP.NET代码(3.5框架):

-- global.asax
    protected void Application_Start(object sender, EventArgs e)
{
    string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
    System.Data.SqlClient.SqlDependency.Start(connectionString);
}

 -- Code-Behind
private DataTable GetAlbums()
{
    string connectionString =
    System.Configuration.ConfigurationManager.ConnectionStrings["UnigoConnection"].ConnectionString;

    DataTable dtAlbums = new DataTable();

    using (SqlConnection connection =
        new SqlConnection(connectionString))
    {
    // Works using select statement, but NOT SP with same text
    //SqlCommand command = new SqlCommand(
    //    "select Id, Artist, Album from dbo.PeteTest", connection);
    SqlCommand command = new SqlCommand();
    command.Connection = connection;
    command.CommandType = CommandType.StoredProcedure;
    command.CommandText = "dbo.spGetPeteTest";


    System.Web.Caching.SqlCacheDependency new_dependency =
        new System.Web.Caching.SqlCacheDependency(command);


    SqlDataAdapter DA1 = new SqlDataAdapter();
    DA1.SelectCommand = command;

    DataSet DS1 = new DataSet();

    DA1.Fill(DS1);

    dtAlbums = DS1.Tables[0];

    Cache.Insert("Albums", dtAlbums, new_dependency);
    }

    return dtAlbums;

}

Anyone have any luck with getting this to work with SPs? 任何人都可以将其与SP一起使用吗? Thanks! 谢谢!

i figured this out, need to set query options BEFORE creating the SP. 我想通了这一点,需要在创建SP之前设置查询选项。 got it working when i created the SP as follows: 当我按如下方式创建SP时,它可以正常工作:

USE [MyDatabase]
GO

set ANSI_NULLS ON
set ANSI_PADDING ON
set ANSI_WARNINGS ON
set CONCAT_NULL_YIELDS_NULL ON
set QUOTED_IDENTIFIER ON
set NUMERIC_ROUNDABORT OFF
set ARITHABORT ON
go


create proc [dbo].[spGetPeteTest]
as

select Id, Artist, Album
from dbo.PeteTest

GO

You are not returning data from the cache every time. 您并非每次都从缓存中返回数据。 It should be like this: 应该是这样的:

if (Cache["Albums"]!=null)
{
   return (DataTable) Cache["Albums"];
}
else
{
  // you need to write coding from database.
}

Note that you cannot use 请注意,您不能使用

with (NOLOCK) 与(NOLOCK)

in the stored procedure or the the dependency will remain constantly invalid. 存储过程中的依赖项将始终保持无效。 This does not appear to be mentioned in the documentation as far as I can tell 据我所知,这似乎没有在文档中提及

I realise that the original poster did not do this but anyone coming here that has the problem stated in the title may have done this so I thought it was worth mentioning. 我意识到原始的发帖人没有这样做,但是来这里遇到标题中所述问题的任何人都可以这样做,所以我认为值得一提。

For me using something like this in the stored proc didn't work. 对我来说,在存储过程中使用类似这样的东西是行不通的。

select id, name from dbo.tblTable;

I had to explicitly put in the references like this. 我必须像这样显式地放入引用。

select dbo.tblTable.id, dbo.tblTable.name from dbo.tblTable;

SQL caching won't work if you use select * , also you need to make sure you put dbo (or relevant schema) in front of your table name. 如果使用select * ,则SQL缓存将不起作用,并且还需要确保将dbo (或相关架构)放在表名的前面。 You can also check SQL profiler to verify if your sql is run hope will help you etc.... 您还可以检查SQL事件探查器以验证您的sql是否运行,希望对您有所帮助...。

Another cause can be this in a SQL statement: 另一个原因可能是在SQL语句中出现的:

AND dbo.[PublishDate] <= GetDate()

The SQLCacheDependency will behave as if the underlying data has changed even if it hasn't, since GetDate() is dynamic (equally if you were to pass DateTime.Now via a @parameter). 由于GetDate()是动态的(如果要通过@parameter传递DateTime.Now,则是动态的), SQLCacheDependency的行为就好像基础数据即使没有更改一样。

This was not obvious to me after re-writing my proc following all the good suggestions above, also not forgetting also to remove "SET NOCOUNT ON" from the proc. 在按照上面所有好的建议重新编写proc之后,这对我来说并不明显,也不要忘记从proc中删除“ SET NOCOUNT ON”。 SQLCacheDependency expires the cache if the data changes OR the query parameters values change, which makes sense I suppose. 如果数据更改查询参数值更改, SQLCacheDependency将使缓存过期,我想这是有道理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM