找回密码
 立即注册

QQ登录

只需一步,快速开始

Lenka.Guo 讲师达人认证 悬赏达人认证
超级版主   /  发表于:2020-8-31 15:13  /   查看:5107  /  回复:8
附件


在线设计器是基于MVC 项目开发的,默认的是将报表保存到服务器的文件夹上面。但往往有很多应用场景是需要将报表保存到数据库是比较常用的应用场景。
所以就需要去修改Starup.cs中reporting文件的配置。

1, 修改app.UseReporting()调用 UserCustomStore,并在 UseCustomerStore中添加 ResourceService 的Model类。
  1.    public void Configuration(IAppBuilder app)
  2.                 {
  3.                         app.UseErrorPage();

  4.                         var dataSetsService = new FileSystemDataSets(DataSetsRootDirectory);
  5.                         var templatesService = new FileSystemTemplates(TemplatesRootDirectory);
  6.                         app.Use((context, next) =>
  7.                         {
  8.                                 context.Set(typeof(IDataSetsService).ToString(), dataSetsService);
  9.                                 context.Set(typeof(ITemplatesService).ToString(), templatesService);
  10.                                 return next.Invoke();
  11.                         });

  12.                         //var resourcesService = new SQLiteResourcesService(Path.Combine(Directory.GetCurrentDirectory(), "ResourcesService" + Path.DirectorySeparatorChar + "reports.db"));
  13.                         var resourcesService = new SQLExpressResourcesService();

  14.                         //app.UseDesigner(config => config.UseFileStore(ResourcesRootDirectory, false));
  15.                         app.UseDesigner(config => {
  16.                                 config.UseCustomStore(resourcesService);
  17.                         });

  18.                         //app.UseReporting(config => config.UseFileStore(ResourcesRootDirectory));
  19.                         app.UseReporting(config => config.UseCustomStore((reportId) =>
  20.                         {
  21.                                 var rep = resourcesService.GetReport(reportId);
  22.                                 return rep;
  23.                         }));

  24.                         app.UseStaticFiles(new StaticFileOptions { FileSystem = new PhysicalFileSystem(String.Format(@"{0}.\wwwroot", HttpRuntime.AppDomainAppPath)) });
  25.                 }
  26.         }
复制代码

2. 新建SQLiteReportService 继承 IResourceService

在这个类里面,我们实现连接 SQLite数据库,并根据报表ID,查找出报表文件后,并将流数据还原成报表对象。

  1. using GrapeCity.ActiveReports.Aspnet.Designer.Services;
  2. using GrapeCity.ActiveReports.PageReportModel;
  3. using GrapeCity.ActiveReports.Rdl.Tools;
  4. using Microsoft.Data.Sqlite;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;

  8. namespace WebDesigner_MVC
  9. {
  10.     public class SQLiteResourcesService : IResourcesService
  11.     {
  12.         private string ConnString { get; set; }

  13.         public SQLiteResourcesService(string dbPath)
  14.         {
  15.             ConnString = $"Filename={dbPath};Mode=ReadWriteCreate";
  16.             using (var conn = new SqliteConnection(ConnString))
  17.             {
  18.                 conn.Open();
  19.                 using (var cmd = conn.CreateCommand())
  20.                 {
  21.                     cmd.CommandText = "CREATE TABLE  IF NOT EXISTS reports(id INTEGER PRIMARY KEY, name TEXT, layout BLOB, temp INTEGER, reportType TEXT);";
  22.                     cmd.ExecuteNonQuery();
  23.                 }
  24.             }
  25.         }

  26.         public void DeleteReport(string id)
  27.         {
  28.             using (var conn = new SqliteConnection(ConnString))
  29.             {
  30.                 conn.Open();
  31.                 using (var cmd = conn.CreateCommand())
  32.                 {
  33.                     cmd.CommandText = $"Delete from reports where id={id}";
  34.                     cmd.ExecuteNonQuery();
  35.                 }
  36.             }
  37.         }

  38.         public Uri GetBaseUri()
  39.         {
  40.             throw new NotImplementedException();
  41.         }

  42.         public byte[] GetImage(string id, out string mimeType)
  43.         {
  44.             throw new NotImplementedException();
  45.         }

  46.         public IImageInfo[] GetImagesList()
  47.         {
  48.             return new IImageInfo[] { };
  49.         }

  50.         public Report GetReport(string id)
  51.         {
  52.             // work-around the report processing that automatically adds .rdlx extension
  53.             if (id.EndsWith(".rdlx"))
  54.             {
  55.                 id = id.Substring(0, id.IndexOf(".rdlx"));
  56.             }
  57.             using (var conn = new SqliteConnection(ConnString))
  58.             {
  59.                 conn.Open();
  60.                 using (var cmd = conn.CreateCommand())
  61.                 {
  62.                     cmd.CommandText =
  63.                     $"SELECT id, layout from reports where id = $id";
  64.                     cmd.Parameters.AddWithValue("$id", id);
  65.                     using (var reader = cmd.ExecuteReader())
  66.                     {
  67.                         while (reader.Read())
  68.                         {
  69.                             using (var readStream = reader.GetStream(1))
  70.                             using (var outputStream = new MemoryStream())
  71.                             {
  72.                                 readStream.CopyTo(outputStream);
  73.                                 return ReportConverter.FromXML(outputStream.ToArray());
  74.                             }
  75.                         }
  76.                     }

  77.                 }
  78.             }
  79.             return null;
  80.         }

  81.         public IReportInfo[] GetReportsList()
  82.         {
  83.             var reportList = new List<ReportInfo>();
  84.             using (var conn = new SqliteConnection(ConnString))
  85.             {
  86.                 conn.Open();
  87.                 using (var cmd = conn.CreateCommand())
  88.                 {
  89.                     cmd.CommandText = "Select id, name, reportType from reports where temp = 0";
  90.                     using (var reader = cmd.ExecuteReader())
  91.                     {
  92.                         while (reader.Read())
  93.                         {
  94.                             reportList.Add(new ReportInfo() { Id = reader.GetString(0), Name = reader.GetString(1), Type = reader.GetString(2) });
  95.                         }
  96.                     }
  97.                 }
  98.             }
  99.             return reportList.ToArray();
  100.         }

  101.         public Theme GetTheme(string id)
  102.         {
  103.             throw new NotImplementedException();
  104.         }

  105.         public IThemeInfo[] GetThemesList()
  106.         {
  107.             return new IThemeInfo[] { };
  108.         }

  109.         public string SaveReport(string name, Report report, bool isTemporary = false)
  110.         {
  111.             var rdlBytes = ReportConverter.ToXml(report);
  112.             string rptType = report.Body.ReportItems.Count > 0 && report.Body.ReportItems[0].GetReportItemTypeName() == "FixedPage" ? "FPL" : "CPL";
  113.             using (var conn = new SqliteConnection(ConnString))
  114.             using (var repStream = new MemoryStream(rdlBytes))
  115.             {
  116.                 conn.Open();
  117.                 var insertCommand = conn.CreateCommand();
  118.                 insertCommand.CommandText =
  119.                 $"INSERT INTO reports(name, layout, temp, reportType) VALUES ("{name}", zeroblob($length), {(isTemporary ? 1 : 0)}, "{rptType}"); SELECT last_insert_rowid();";
  120.                 insertCommand.Parameters.AddWithValue("$length", repStream.Length);
  121.                 var rowid = (long)insertCommand.ExecuteScalar();
  122.                 using (var writeStream = new SqliteBlob(conn, "reports", "layout", rowid))
  123.                 {
  124.                     repStream.CopyTo(writeStream);
  125.                 }
  126.                 return rowid.ToString();
  127.             }

  128.         }

  129.         public string UpdateReport(string id, Report report)
  130.         {
  131.             var rdlBytes = ReportConverter.ToXml(report);
  132.             using (var conn = new SqliteConnection(ConnString))
  133.             using (var repStream = new MemoryStream(rdlBytes))
  134.             {
  135.                 conn.Open();
  136.                 using (var writeStream = new SqliteBlob(conn, "reports", "layout", long.Parse(id)))
  137.                 {
  138.                     repStream.CopyTo(writeStream);
  139.                 }
  140.                 return id;
  141.             }

  142.         }
  143.     }
  144. }
复制代码






本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

8 个回复

倒序浏览
Popeye
注册会员   /  发表于:2021-8-5 13:20:04
沙发
好东西,正好需要,先mark一下。
我需要把报表文件及主题文件保存在阿里云的OSS中。
回复 使用道具 举报
James.Lv讲师达人认证 悬赏达人认证 活字格认证 Wyn认证
超级版主   /  发表于:2021-8-5 16:04:53
板凳
Popeye 发表于 2021-8-5 13:20
好东西,正好需要,先mark一下。
我需要把报表文件及主题文件保存在阿里云的OSS中。

回复 使用道具 举报
Popeye
注册会员   /  发表于:2021-8-5 16:45:21
地板
本帖最后由 Popeye 于 2021-8-5 16:46 编辑

James 版主好!我下载了这个范例,但是里面的 SQLExpressResourcesService 和 SQLiteResourcesService 服务类中都没有实现 获取 Theme 和 Image 的方法,请问,能给出完整的范例么?
  1.         public byte[] GetImage(string id, out string mimeType)
  2.         {
  3.             throw new NotImplementedException();
  4.         }

  5.         public IImageInfo[] GetImagesList()
  6.         {
  7.             return new IImageInfo[] { };
  8.         }

  9.         public Theme GetTheme(string id)
  10.         {
  11.             throw new NotImplementedException();
  12.         }

  13.         public IThemeInfo[] GetThemesList()
  14.         {
  15.             return new IThemeInfo[] { };
  16.         }
复制代码
回复 使用道具 举报
James.Lv讲师达人认证 悬赏达人认证 活字格认证 Wyn认证
超级版主   /  发表于:2021-8-6 18:41:44
5#
Popeye 发表于 2021-8-5 16:45
James 版主好!我下载了这个范例,但是里面的 SQLExpressResourcesService 和 SQLiteResourcesService 服务 ...

这个我们这边整理个示例到时候发您
回复 使用道具 举报
Popeye
注册会员   /  发表于:2021-8-14 16:00:31
6#
James.Lv 发表于 2021-8-6 18:41
这个我们这边整理个示例到时候发您

这个ResourceService 我已全部实现完成了,但是遇到一个无法在设计器中预览报表的问题,请麻烦帮我看看吧:


https://gcdn.grapecity.com.cn/fo ... mp;extra=#pid412931
回复 使用道具 举报
James.Lv讲师达人认证 悬赏达人认证 活字格认证 Wyn认证
超级版主   /  发表于:2021-8-16 09:26:56
7#
Popeye 发表于 2021-8-14 16:00
这个ResourceService 我已全部实现完成了,但是遇到一个无法在设计器中预览报表的问题,请麻烦帮我看看吧 ...

好的  我们在您的求助贴中帮您解决
回复 使用道具 举报
wengMQ悬赏达人认证
银牌会员   /  发表于:2022-3-2 23:32:58
8#
获取 Theme 和 Image 的方法,请问,能给出完整的范例么?
回复 使用道具 举报
Bella.YuanWyn认证
超级版主   /  发表于:2022-3-3 12:15:18
9#
wengMQ 发表于 2022-3-2 23:32
获取 Theme 和 Image 的方法,请问,能给出完整的范例么?

您好,该问题已在下面链接回复,稍后整理好发您。
https://gcdn.grapecity.com.cn/fo ... read&tid=142218
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部