Question

I can't get SqlCacheDependency to work with a simple stored proc (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):

-- 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? Thanks!

Was it helpful?

Solution

i figured this out, need to set query options BEFORE creating the SP. got it working when i created the SP as follows:

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

OTHER TIPS

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.
}

Another cause can be this in a SQL statement:

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).

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. SQLCacheDependency expires the cache if the data changes OR the query parameters values change, which makes sense I suppose.

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. You can also check SQL profiler to verify if your sql is run hope will help you etc....

Note that you cannot use

with (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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top