This example shows how to configure report caching. Here is three examples of caching report - Default, File, MS SQL:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Configure_Report_Caching.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Configuring Report caching</title>
</head>
<body>
<h2>Configuring Report Caching</h2>
<form id="form1" runat="server">
<br /><a href="/DefaultCache.aspx">Default Cache</a>
<br /><a href="/FileCache.aspx">File Cache</a>
<br /><a href="/MSSQLCache.aspx">MS SQL Cache</a>
</form>
</body>
</html>
Let's start with Default caching. First, create
StiDefaultCacheHelper
class:
public class StiDefaultCacheHelper : StiCacheHelper
{
public override StiReport GetReport(string guid)
{
return base.GetReport(guid);
}
public override void SaveReport(StiReport report, string guid)
{
base.SaveReport(report, guid);
}
public override void RemoveReport(string guid)
{
base.RemoveReport(guid);
}
}
Then, create
DefaultCache
method:
public DefaultCache() {
StiWebViewer.CacheHelper = new StiDefaultCacheHelper();
}
Finally, create report loading method:
protected void StiWebViewer1_GetReport(object sender, StiReportDataEventArgs e)
{
var report = new StiReport();
report.Load(Server.MapPath("Reports/SimpleList.mrt"));
e.Report = report;
}
Next, File caching. First, create
StiFileCacheHelper
class:
public class StiFileCacheHelper : StiCacheHelper
{
public override StiReport GetReport(string guid)
{
var path = HttpContext.Server.MapPath(Path.Combine("CacheFiles", guid));
if (File.Exists(path))
{
var report = new StiReport();
var packedReport = File.ReadAllText(path);
if (guid.EndsWith(GUID_ReportTemplate)) report.LoadPackedReportFromString(packedReport);
else report.LoadPackedDocumentFromString(packedReport);
return report;
}
return null;
}
public override void SaveReport(StiReport report, string guid)
{
var packedReport = guid.EndsWith(GUID_ReportTemplate) ? report.SavePackedReportToString() : report.SavePackedDocumentToString();
var path = HttpContext.Server.MapPath(Path.Combine("CacheFiles", guid));
File.WriteAllText(path, packedReport);
}
public override void RemoveReport(string guid)
{
var path = HttpContext.Server.MapPath(Path.Combine("CacheFiles", guid));
if (File.Exists(path))
File.Delete(path);
}
}
Then, create
FileCache
method:
public FileCache()
{
StiWebViewer.CacheHelper = new StiFileCacheHelper();
}
Finally, create report loading method:
protected void StiWebViewer1_GetReport(object sender, StiReportDataEventArgs e)
{
var report = new StiReport();
report.Load(Server.MapPath("Reports/SimpleList.mrt"));
e.Report = report;
}
The last, MS SQL caching. First, create
StiMSSQLCacheHelper
class:
public class StiMSSQLCacheHelper : StiCacheHelper
{
// Please use your own database connection
private string connectionString = @"Data Source=127.0.0.1;Initial Catalog=SampleDB;Integrated Security=False;User ID=******;Password=******;";
public override StiReport GetReport(string guid)
{
var connection = new SqlConnection(connectionString);
try
{
connection.Open();
var query = $"SELECT Guid, Value FROM ReportCache WHERE Guid='{guid}'";
var command = new SqlCommand(query, connection);
var reader = command.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
var packedReport = reader.GetString(1);
reader.Close();
var report = new StiReport();
if (guid.EndsWith(GUID_ReportTemplate)) report.LoadPackedReportFromString(packedReport);
else report.LoadPackedDocumentFromString(packedReport);
return report;
}
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
connection.Close();
}
return null;
}
public override void SaveReport(StiReport report, string guid)
{
var connection = new SqlConnection(connectionString);
try
{
connection.Open();
var query = $"DELETE FROM ReportCache WHERE Guid='{guid}'";
var command = new SqlCommand(query, connection);
command.ExecuteNonQuery();
var packedReport = guid.EndsWith(GUID_ReportTemplate) ? report.SavePackedReportToString() : report.SavePackedDocumentToString();
query = $"INSERT INTO ReportCache (Guid, Value) VALUES ('{guid}', '{packedReport}')";
command = new SqlCommand(query, connection);
command.ExecuteNonQuery();
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
connection.Close();
}
}
public override void RemoveReport(string guid)
{
var connection = new SqlConnection(connectionString);
try
{
connection.Open();
var query = $"DELETE FROM ReportCache WHERE Guid='{guid}'";
var command = new SqlCommand(query, connection);
command.ExecuteNonQuery();
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
connection.Close();
}
}
}
Then, create
MSSQLCache
method:
public MSSQLCache()
{
StiWebViewer.CacheHelper = new StiMSSQLCacheHelper();
}
Finally, create report loading method:
protected void StiWebViewer1_GetReport(object sender, StiReportDataEventArgs e)
{
var report = new StiReport();
report.Load(Server.MapPath("Reports/SimpleList.mrt"));
e.Report = report;
}
На скриншоте ниже Вы можете увидеть результат выполнения данного кода: