我将表数据加载到缓存中。并将其绑定到dropdown。但是如果在datatable中添加了任何新值,则该缓存应该无效并且新缓存应该得到create.I正在使用Asp.Net 3.5,linq to SQL < / p>
Folloing是我的.aspx代码
public List<Employee> GetEmployeeData()
{
//get from cache
object obj = HttpRuntime.Cache.Get(Constants.Constants.cacheEmployee);
if (obj != null)
{
return obj as List<Employee>;
}
else // get from database
{
using (TestdbEntities entities = new TestdbEntities(connectionString))
{
try
{
IEnumerable<Employee> employee= entities.Employees.OrderBy(l => l.EmployeeName);
System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(connectionString);
if (!System.Web.Caching.SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(connectionString).Contains ("master.Employee"))
System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(connectionString, "master.Employee");
System.Web.Caching.SqlCacheDependency sqldep = new System.Web.Caching.SqlCacheDependency(connectionString, "master.Employee"); ;
HttpRuntime.Cache.Insert(Constants.Constants.cacheEmployee, employee.ToList(), sqldep);
return employee.ToList();
}
catch (Exception ex)
{
throw ex;
}
}
}
}
但我收到了以下错误
请确保数据库名称和表名称有效。表名必须符合SQL中常规标识符的格式。
请建议
答案 0 :(得分:1)
另一个答案是正确的“。”在表名中是问题。由于某种原因,没有编写AspNet_SqlCacheRegisterTableStoredProcedure来处理不同的模式。
我必须把这个链接归功于这个问题: http://forums.asp.net/t/1249281.aspx?SQL+Cache+Dependency+with+custom+schema
然而,我想出了我自己重新编写下面的存储过程,这是一个更好的:)它有点干净,处理情况,即使[包括在名称中,并保持架构名称AspNet_SqlCacheTablesForChangeNotification表,以防您在两个不同的模式中具有相同的表名。
DECLARE @triggerName AS NVARCHAR(3000)
DECLARE @fullTriggerName AS NVARCHAR(3000)
DECLARE @canonTableName NVARCHAR(3000)
DECLARE @quotedTableName NVARCHAR(3000)
DECLARE @schemaName NVARCHAR(3000)
DECLARE @schemaAndTableName NVARCHAR(3000)
--Replace these if they are in the param, we'll add them back later for the @canonTableName only
SET @tableName = REPLACE(REPLACE(@tableName, '[', ''), ']', '')
IF(CHARINDEX('.',@tableName) <> 0)
BEGIN
SET @schemaName = SUBSTRING(@tableName,0,CHARINDEX('.',@tableName))
SET @tableName = SUBSTRING(@tableName,CHARINDEX('.',@tableName) + 1,LEN(@tableName) - CHARINDEX('.',@tableName))
SET @schemaAndTableName = @schemaName + '.' + @tableName
END
ELSE
BEGIN
SET @schemaName = 'dbo' --If the param doesn't have a schema, use the default
SET @schemaAndTableName = @tableName --If they didn't pass in the schema, we don't want it in the AspNet_SqlCacheTablesForChangeNotification table
END
SET @triggerName = @tableName + '_AspNet_SqlCacheNotification_Trigger'
SET @fullTriggerName = '[' + @schemaName + '].[' + @triggerName + ']'
SET @canonTableName = '[' + @schemaName + '].[' + @tableName + ']'
/* First make sure the table exists */
IF (SELECT OBJECT_ID(@schemaAndTableName, 'U')) IS NULL
BEGIN
RAISERROR ('00000001', 16, 1)
RETURN
END
BEGIN TRAN
/* Insert the value into the notification table */
IF NOT EXISTS (SELECT tableName FROM dbo.AspNet_SqlCacheTablesForChangeNotification WITH (NOLOCK) WHERE tableName = @schemaAndTableName)
IF NOT EXISTS (SELECT tableName FROM dbo.AspNet_SqlCacheTablesForChangeNotification WITH (TABLOCKX) WHERE tableName = @schemaAndTableName)
INSERT dbo.AspNet_SqlCacheTablesForChangeNotification
VALUES (@schemaAndTableName, GETDATE(), 0)
/* Create the trigger */
SET @quotedTableName = QUOTENAME(@schemaAndTableName, '''')
IF NOT EXISTS (SELECT name FROM sysobjects WITH (NOLOCK) WHERE name = @triggerName AND type = 'TR')
IF NOT EXISTS (SELECT name FROM sysobjects WITH (TABLOCKX) WHERE name = @triggerName AND type = 'TR')
EXEC('CREATE TRIGGER ' + @fullTriggerName + ' ON ' + @canonTableName +'
FOR INSERT, UPDATE, DELETE AS BEGIN
SET NOCOUNT ON
EXEC dbo.AspNet_SqlCacheUpdateChangeIdStoredProcedure N' + @quotedTableName + '
END
')
COMMIT TRAN
答案 1 :(得分:0)
问题是表名.
中的master.Employee
。 EnableTableForNotifications
方法调用数据库中的存储过程AspNet_SqlCacheRegisterTableStoredProcedure
。在里面是对OBJECT_ID
函数的调用,我认为这是将表名解析为master数据库中名为Employee的表。 (当我第一次看到这个问题时,我的想法是一样的。)
如果您要使用表名中的下划线.
替换_
,那么您的代码将正确运行。
(您可能还希望在数据不在缓存中的情况下检查您的代码;数据可能由于多种原因从缓存中消失,但目前您的代码每次都会启用数据库以进行通知,无论它是否已经启用。Single Responsibility Principle适用于GetEmployeeData
方法;它只应关注从缓存或数据库获取数据而不是设置缓存和通知,该代码应分解为单独的方法。)