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

I created a C # .net application that uses dates from a SQL Server 2008 database table. Is there a way to temporarily store data so that my program does not require multiple server calls for the same set of information? I know how to get the information I need and create a temporary data set, however, it is only available for a specific method or class, and then it leaves. I need the results to be publicly available before the program closes.

This is what I still have, and I'm 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 say the dataset is named "activeDataset"
+3
source share
7 answers

/, HttpRuntime.Cache ( 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;
}

, IoC .

+4

SQLCacheDependency. MSDN

+1

, ( ?).

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

...

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

Cache . , SqlDependency, , . , , 4 , . Cache.Insert()

+1

, .

+1

:

  • - ( singleton "" )
  • Use caching - you can use the Dictionary and regular string keys to detect ( ContainsKeymethod) whether the data is already selected or if a sql server call is required. This can be useful when you need different DataSets. The dictionary is pretty fast.
+1
source

Source: https://habr.com/ru/post/1725585/


All Articles