简体   繁体   English

我可以暂时将数据存储在我的C#.Net应用程序中,以减少从SQL Server调用数据的需要吗?

[英]Can I temporarily store data in my C#.Net app to reduce the need for calls for data from SQL Server?

I created a C#.net app that uses dates from a SQL Server 2008 database table. 我创建了一个C#.net应用程序,它使用SQL Server 2008数据库表中的日期。 Is there a way for me to temporarily store the data so that my program does not have to repeatedly make server calls for the same set of information? 有没有办法暂时存储数据,以便我的程序不必为同一组信息重复进行服务器调用? I know how to pull the info I need and create a temporary dataset, however, it is only accessible to the particular method or class and then goes away. 我知道如何提取我需要的信息并创建一个临时数据集,但是,只有特定方法或类可以访问它然后消失。 I need the results to be universally available until the program closes. 在程序结束之前,我需要将结果普遍可用。

This is what I have so far and I am not sure where to go next: 这是我到目前为止所做的,我不知道下一步该怎么做:

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"

If you are going to use key/value pair caching, I recommend you use HttpRuntime.Cache (available outside ASP.NET applications) since it already does alot of work for you. 如果您打算使用键/值对缓存,我建议您使用HttpRuntime.Cache (在ASP.NET应用程序之外可用),因为它已经为您做了很多工作。

In it's simplest implementation: 在它最简单的实现:

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

Having said that, I usually use IoC trickery to create a caching layer in front of my repository for the sake of cohesion. 话虽如此,为了凝聚力,我通常使用IoC技巧在我的存储库前面创建一个缓存层。

You could create a singleton object, and store the data in this object. 您可以创建单个对象,并将数据存储在此对象中。

Be aware that there is a lot more to single ton objects that you will have to think about. 请注意,您需要考虑的单吨物体还有很多。

Have a look at 看一下

You should use SQLCacheDependency. 您应该使用SQLCacheDependency。 Take a look at MSDN 看看MSDN

You could store the datatable in a static variable that would be accesible from any part of your code (is this necessary?). 您可以将数据表存储在一个静态变量中,该变量可以从代码的任何部分访问(这是必要的吗?)。

public class MyDataSetCache
{
    public static DataSet MyDataSet { get; set; }
}

Some other code... 其他一些代码......

// SQL Statements....
MyDataSetCache.MyDataSet = activeDataset // Editted to follow OP :-)

You can definately use Cache to reduce database hits, Besides using SqlDependency you can have a cache based on time. 您可以肯定使用Cache来减少数据库命中,除了使用SqlDependency之外,您还可以根据时间使用缓存。 You can invalidate your cache let's say every 4 hours,and hit the database again. 您可以每隔4小时使缓存无效,然后再次访问数据库。 Check out Cache.Insert() 查看Cache.Insert()

我通常将整个对象序列化为一个文件,并在转到数据库之前尝试先读取它。

You can use a set of implementation hooks to achieve result: 您可以使用一组实现挂钩来实现结果:

  1. Common data-application layer (data singleton or some data coupling using static class with lesser "visible" methods' dependencies) 通用数据应用层(数据单例或某些数据耦合使用静态类,具有较小的“可见”方法依赖性)
  2. Use caching -- you can use Dictionary and common string-keys to detect ( ContainsKey method) whether data is already fetched or needs sql-server call. 使用缓存 - 您可以使用Dictionary和公共字符串键来检测( ContainsKey方法)数据是否已被提取或需要sql-server调用。 This can be useful when you need different DataSet s. 当您需要不同的DataSet时,这可能很有用。 Dictionary works pretty fast. 字典工作得非常快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何存储我的数据(C#.net) - How to store my data (C#.net) 将数据从 c#.net 应用程序发送到另一个桌面应用程序 - Sending data from c#.net app to another desktop app 如何从 Microsoft Graph API 中提取 Planner 信息并将数据插入到我的 C#.NET Windows 表单中? - How can I pull Planner information from the Microsoft Graph API and insert the data into my C#.NET Windows form? 在C#.NET的帮助下,将数据从我的计算机连接到服务器的MySQL数据库表 - Connect and insert data from my computer to server's MySQL DB table with the help of C#.NET 使用 C#.NET 从 SQL 向 Oracle 批量插入数据 - Bulk data insert from SQL to Oracle using C#.NET 从C#.net中的服务器连续读取数据 - Continuously read data from a server in C#.net 为什么不能将这些数据导入我的RSACryptoServiceProvider? (C#.NET) - Why can't I import this data to my RSACryptoServiceProvider? (C#.NET) 需要建议每隔5秒从sql server查询数据并将其发送到其他应用程序。(。NET C#) - Need advice to query data from sql server on every 5 seconds and send it to other app.(.NET C#) 如何将大容量数据从数据库表填充到组合框? -C#.NET - How Can i Populate Bulk of Data from a DB Table into a Combo Box? - C#.NET 我如何使用C#.net通过存储过程将pdf文件存储在Sql Server中 - how could i store pdf file in Ms Sql Server through Stored Procedure using C#.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM