0

我无法让 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

这是我的 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;

}

任何人都可以让这个与 SP 一起工作吗?谢谢!

4

6 回答 6

2

我想通了,需要在创建 SP 之前设置查询选项。当我按如下方式创建 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
于 2010-03-01T16:22:32.910 回答
0

您不会每次都从缓存中返回数据。它应该是这样的:

if (Cache["Albums"]!=null)
{
   return (DataTable) Cache["Albums"];
}
else
{
  // you need to write coding from database.
}
于 2010-02-23T09:22:16.623 回答
0

对我来说,在存储过程中使用这样的东西是行不通的。

select id, name from dbo.tblTable;

我必须像这样明确地输入参考资料。

select dbo.tblTable.id, dbo.tblTable.name from dbo.tblTable;
于 2010-09-24T15:22:22.043 回答
0

如果您使用 SQL 缓存将不起作用,您select *还需要确保将dbo(或相关架构)放在表名前面。您还可以检查 SQL 探查器以验证您sql是否正在运行希望对您有所帮助等......

于 2010-12-01T00:57:29.540 回答
0

另一个原因可能是在 SQL 语句中:

AND dbo.[PublishDate] <= GetDate()

SQLCacheDependency将表现得好像基础数据已更改,即使它没有更改,因为 GetDate() 是动态的(同样,如果您要通过 @parameter 传递 DateTime.Now)。

在按照上面所有好的建议重写我的过程后,这对我来说并不明显,也不要忘记从过程中删除“SET NOCOUNT ON”。如果数据更改或查询参数值更改, SQLCacheDependency会使缓存过期,我想这是有道理的。

于 2012-03-07T20:46:30.707 回答
0

请注意,您不能使用

与 (NOLOCK)

在存储过程中或依赖项将始终保持无效。据我所知,这似乎没有在文档中提到

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.

于 2015-04-29T15:29:04.290 回答