人生就是修炼 发表于 2023-5-5 09:51:29

请问使用SQLite数据库,如何动态绑定数据源,实现打印功能

C# Winform原先使用的是Access数据库,可以动态绑定,代码如下
            string file_name = @"1笔录.rdlx";
            GrapeCity.ActiveReports.PageReport pageReport = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(file_name));
            pageReport.Report.DataSources.DataSourceReference = "";
            pageReport.Report.DataSources.ConnectionProperties.DataProvider = "OLEDB";
            pageReport.Report.DataSources.ConnectionProperties.ConnectString = string.Format(connectString);
            //var _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(pageReport);
            GrapeCity.ActiveReports.Document.PageDocument reportDocument = new GrapeCity.ActiveReports.Document.PageDocument(pageReport);
            pageReport.Report.ReportParameters.DefaultValue.Values.Clear();   
            pageReport.Report.ReportParameters.DefaultValue.Values.Add(this.询问号.Text.Trim());
            pageReport.Report.ReportParameters.DefaultValue.Values.Clear();
            pageReport.Report.ReportParameters.DefaultValue.Values.Add(this.年度.Text.Trim());
            this.viewer打印.LoadDocument(reportDocument);
在使用了SQLite数据库时如何编写?
尝试了将
            pageReport.Report.DataSources.ConnectionProperties.DataProvider = "OLEDB";
换成了
            pageReport.Report.DataSources.ConnectionProperties.DataProvider = "ODBC";

        pageReport.Report.DataSources.ConnectionProperties.DataProvider = "SQLite";
都不能成功
请高手指点,谢谢!

人生就是修炼 发表于 2023-5-5 09:51:30

本帖最后由 人生就是修炼 于 2023-5-8 14:02 编辑

问题已经解决:
原来使用字符串链接Access数据库和SQLite数据库的写法不一样的,对于代码中动态绑定数据源这个是必须掌握的知识。

Access格式为:
private readonly string connectString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = YiFa.accdb;Persist Security Info=False;";
SQLite格式为:
private readonly string connectString = @"DRIVER=SQLITE3 ODBC DRIVER;DATABASE=data\YiFa.db";

另外去掉下面两行代码也不会报错

pageReport.Report.DataSources.DataSourceReference = "";
pageReport.Report.DataSources.ConnectionProperties.DataProvider = "ODBC";
完整代码如下:
private readonly string connectString = @"DRIVER=SQLITE3 ODBC DRIVER;DATABASE=data\YiFa.db";

string file_name = @"笔录.rdlx";
GrapeCity.ActiveReports.PageReport pageReport = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(file_name));
pageReport.Report.DataSources.ConnectionProperties.ConnectString = string.Format(connectString);
GrapeCity.ActiveReports.Document.PageDocument reportDocument = new GrapeCity.ActiveReports.Document.PageDocument(pageReport);
pageReport.Report.ReportParameters.DefaultValue.Values.Clear();   
pageReport.Report.ReportParameters.DefaultValue.Values.Add(this.询问号.Text.Trim());
pageReport.Report.ReportParameters.DefaultValue.Values.Clear();
pageReport.Report.ReportParameters.DefaultValue.Values.Add(this.年度.Text.Trim());
this.viewer打印.LoadDocument(reportDocument);总结:不同的数据库,对数据源的调用写法是不一样的,这里又总结了一种写法

Bella.Yuan 发表于 2023-5-5 11:39:48

您好,动态数据源绑定可以参考这个链接:通过 DataSet 类型数据源绑定 DataTable - ActiveReports 报表控件用户手册 - 葡萄城产品文档中心 (grapecity.com.cn)
c#连接SQLite数据库参考链接:https://www.cnblogs.com/icebutterfly/p/7850689.html
打印功能参考链接:打印方法 - ActiveReports 报表控件用户手册 - 葡萄城产品文档中心 (grapecity.com.cn)

Bella.Yuan 发表于 2023-5-5 17:26:09

人生就是修炼 发表于 2023-5-5 16:56
问题已经解决:
原来使用字符串链接Access数据库和SQLite数据库的写法不一样的,对于代码中动态绑定数据源这 ...

非常感谢您分享该问题的解决方法,给您点个赞,{:5_117:}那本帖就先结帖了,有问题您另开新帖哦~{:5_118:}
页: [1]
查看完整版本: 请问使用SQLite数据库,如何动态绑定数据源,实现打印功能