我是否可以将数据临时存储在C#.Net应用程序中,以减少从SQL Server调用数据的需要?
我创建了一个C#.net应用程序,它使用SQLServer2008数据库表中的日期。有没有一种方法可以让我临时存储数据,这样我的程序就不必重复地对同一组信息进行服务器调用?我知道如何提取所需的信息并创建临时数据集,但是,它只能由特定的方法或类访问,然后就消失了。我需要的结果是普遍可用,直到程序关闭 这就是我目前所拥有的,我不确定下一步该去哪里:我是否可以将数据临时存储在C#.Net应用程序中,以减少从SQL Server调用数据的需要?,c#,sql,sql-server,ado.net,C#,Sql,Sql Server,Ado.net,我创建了一个C#.net应用程序,它使用SQLServer2008数据库表中的日期。有没有一种方法可以让我临时存储数据,这样我的程序就不必重复地对同一组信息进行服务器调用?我知道如何提取所需的信息并创建临时数据集,但是,它只能由特定的方法或类访问,然后就消失了。我需要的结果是普遍可用,直到程序关闭 这就是我目前所拥有的,我不确定下一步该去哪里: SqlConnection ReportConnect = new SqlConnection(ConnectionString); String re
SqlConnection ReportConnect = new SqlConnection(ConnectionString);
String reportQuery = @"SELECT DISTINCT DATE FROM dbo.myTable ORDER BY DATE DESC";
ReportConnect.Open();
SqlCommand cmd = ReportConnect.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.Connection = ReportConnect;
cmd.CommandText = reportQuery.ToString();
SqlDataReader rdr = cmd.ExecuteReader();
while(rdr.Read()) {
//I can access the results here
}
//how do I add this data for the life of the program instance to my current
//dataset. Let's say the dataset is named "activeDataset"
您可以创建一个单例对象,并将数据存储在此对象中 请注意,对于单吨对象,您需要考虑的还有很多 看看
// SQL Statements....
MyDataSetCache.MyDataSet = activeDataset // Editted to follow OP :-)
您可以明确地使用缓存来减少数据库命中,除了使用SqlDependency之外,您还可以基于时间使用缓存。你可以使你的缓存失效,比如说每4小时一次,然后再次访问数据库。签出Cache.Insert()我通常将整个对象序列化为一个文件,并在转到数据库之前尝试先读取它。您可以使用一组实现挂钩来实现以下结果:
ContainsKey
method)数据是否已经获取或需要sql server调用。当您需要不同的数据集时,这可能很有用。这本字典很快就可以用了
如果要使用键/值对缓存,我建议您使用(在ASP.NET应用程序之外可用),因为它已经为您做了很多工作 在最简单的实现中:
public IList<DateTime> GetUniqueDates()
{
const string CacheKey = "RepositoryName.UniqueDates";
Cache cache = HttpRuntime.Cache;
List<DateTime> result = cache.Get[CacheKey] as List<DateTime>;
if (result == null)
{
// If you're application has multithreaded access to data, you might want to
// put a double lock check in here
using (SqlConnection reportConnect = new SqlConnection(ConnectionString))
{
// ...
result = new List<DateTime>();
while(reader.Read())
{
result.Add((DateTime)reader["Value"]);
}
}
// You can specify various timeout options here
cache.Insert(CacheKey, result);
}
return result;
}
public IList GetUniqueDates()
{
常量字符串CacheKey=“RepositoryName.UniqueDates”;
Cache Cache=HttpRuntime.Cache;
列表结果=缓存。获取[CacheKey]作为列表;
如果(结果==null)
{
//若您的应用程序对数据具有多线程访问权限,则可能需要
//在这里放一张双锁支票
正在使用(SqlConnection reportConnect=newsqlconnection(ConnectionString))
{
// ...
结果=新列表();
while(reader.Read())
{
结果。添加((日期时间)读取器[“值”]);
}
}
//您可以在此处指定各种超时选项
插入(CacheKey,result);
}
返回结果;
}
话虽如此,为了内聚,我通常使用IoC技巧在我的存储库前面创建一个缓存层。+1感谢您的帮助和详细解释。是的,细节对我来说是必要的。谢谢你把它们包括进来。很高兴我能帮上忙。听起来这不是个问题,但上面的代码不一定是线程安全的。@astander答案中的链接将更详细地介绍这一点,应该对您有用。我考虑过这样做。访问文本或xml文件是否比sql server快得多?我想是的,但我还没试过看呢?当然。本地呼叫总是比远程呼叫快。对不起,沃尔坎,我意外地在错误的答案中添加了一个代码示例;)这将被称为缓存数据。
ObjectCache
/MemoryCache
是最适合使用的post-.NET 4对象,也可在ASP.NET之外使用
public IList<DateTime> GetUniqueDates()
{
const string CacheKey = "RepositoryName.UniqueDates";
Cache cache = HttpRuntime.Cache;
List<DateTime> result = cache.Get[CacheKey] as List<DateTime>;
if (result == null)
{
// If you're application has multithreaded access to data, you might want to
// put a double lock check in here
using (SqlConnection reportConnect = new SqlConnection(ConnectionString))
{
// ...
result = new List<DateTime>();
while(reader.Read())
{
result.Add((DateTime)reader["Value"]);
}
}
// You can specify various timeout options here
cache.Insert(CacheKey, result);
}
return result;
}