How to use DB connectors from the Nuget repository

2018-05-28

FastReport.Net has been supplying plugins for a long time - connectors to various databases. These plugins allow you to add connectors to the report designer to connect to the required database. The purpose of creating these plug-in connectors is to simplify the process of connecting to a data source.

However, there is one inconvenience in using such plug-ins - they must first be compiled. And for this you must first download third-party libraries from the developer's database site. But, once you have compiled the plug-in, you can use it for as long as you like.

FastReport now distributes these connectors in the form of libraries through the Nuget package manager. This is true both for the .Net Framework 4 application and for ASP .Net Core. Now we are spared the need to compile plugins with third-party libraries, for which many thanks to the developers of FastReport.

Let's talk about the peculiarities of using these libraries. To create a report, you need to call the designer. With plugins, you would use a separate Report Designer program, which is convenient. But with the installed connector libraries, you'll need to call a designer from your application to use these connectors in your report.

If for WinForms applications to call the report designer from the code is a trifle, then in .Net Core applications it is impossible. Of course, there is Online Designer for web development of reports, but this is a completely different story. Therefore, you must first create a WinForms application, install the required connector into it and call the report designer. Or you can use the already compiled Designer.exe application that comes with FastReport, but in this case the list of supported DBMS will be limited (MS SQL is supported).

Let's now take a look at all this with an example. Create a test WinForms application and add FastReport.Net libraries to it. And also try to install connectors from the manager Nuget.

The figure shows that we have connectors to the databases: MsSql, MySql, Postgres, SQLite, Json, MongoDB, RavenDB.

Let's try to connect to the Json data source. Install the appropriate connector from Nuget.

On the form, add a button and the following code for it:

1
2
3
4
5
6
7
8
9
10
using FastReport;
using FastReport.Data;
using FastReport.Utils;

 private void button1_Click(object sender, EventArgs e)
 {
 new JsonAssemblyInitializer();
 Report report = new Report();
 report.Design();
 }

 In the first line of the button click event handler, we initialize the added JSON connector. Then create a report object and call the designer.

In the designer, we create a new data connection. And among the available connectors we see the JSON we added. Select it and set the path to the data file with the .json extension.

 

Create a report template and save it. Now, in the code for our button, you can replace the line:

1
report.Design();

 to:

1
2
report.Load(@"TestJsonConnection.frx");
report.Show();

 Here we specify the path to the report file that we created earlier and run the report for the show.

Thus, using the library-connector required from us only two actions - to install the library from Nuget, and to initialize it in the code.

However, not everything is so simple if it comes from ASP .Net Core application.

Firstly, we need to create a report. And for this we need to run the designer from the code. In web projects, this is impossible. I'll have to create a WinForms application, specifically to run the designer. Either install the plugin in a separate ReportDesigner and create reports in it.

Secondly, the program code is somewhat more complicated. In addition to initializing the connector library, you will need to specify a connection string, create tables, and add a connection to the report:

1
2
3
4
 JsonDataConnection conn = new JsonDataConnection();
 conn.ConnectionString = "Json=\"K:\\Documents\\nwind.json\"";
 conn.CreateAllTables();
 report.Dictionary.Connections.Add(conn);

 By labor, this is comparable with the use of third-party libraries for connecting to databases. So, in the case of .Net Core, not everything is so unambiguous. But in the .Net Framework 4 applications, the advantages over third-party libraries are obvious.

November 01, 2024

New Features of the FastReport VCL Editor

We are considering new features of the report editor: extension lines, highlighting of intersecting objects, updated report and data trees.
October 30, 2024

Using Styles When Creating Reports in FastReport VCL

The article discusses one of the new features of FastReport VCL — the use of styles and style sheets.
October 28, 2024

How to Set Up WSL 2 for Working with FastReport and FastCube

In this article, we will explore how to set up WSL 2 for working with FastReport and FastCube components in Lazarus for Linux.
Fast Reports
  • 800-985-8986 (English, US)
  • +4930568373928 (German)
  • +55 19 98147-8148 (Portuguese)
  • info@fast-report.com
  • 66 Canal Center Plaza, Ste 505, Alexandria, VA 22314

© 1998-2024 Fast Reports Inc.