建议缓存静态数据以减轻数据库的负载!
您可以通过多种方式缓存应用程序的数据。一种简单的方法是使用静态变量,但我建议在更简单的场景中使用这样的应用程序状态:
Application["key"] = value;
object value = Application["key"];
要使缓存失效,您也可以在缓存中存储一个时间戳,并检查是否经过了 5 分钟(或 1 天或其他时间),然后从数据库中重新加载数据。
或使用 HttpRuntime 缓存功能:
[System.ComponentModel.DataObject]
public class StaticCache
{
public static void LoadStaticCache()
{
// Get suppliers - cache using the data cache
SuppliersBLL suppliersBLL = new SuppliersBLL();
HttpRuntime.Cache.Insert(
/* key */ "key",
/* value */ suppliers,
/* dependencies */ null,
/* absoluteExpiration */ Cache.NoAbsoluteExpiration,
/* slidingExpiration */ Cache.NoSlidingExpiration,
/* priority */ CacheItemPriority.NotRemovable,
/* onRemoveCallback */ null);
}
[DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
public static Northwind.SuppliersDataTable GetSuppliers()
{
return HttpRuntime.Cache["key"] as Northwind.SuppliersDataTable;
}
}
要在启动时加载数据,请在 Global.asax 中进行:
void Application_Start(object sender, EventArgs e)
{
StaticCache.LoadStaticCache();
}
我建议您阅读:http://www.asp.net/web-forms/tutorials/data-access/caching-data/caching-data-at-application-startup-cs 了解更多信息。