Need a special offer?Find out if your project fits.
+

Connecting to a relational database with .NET Core

NOTE: Flexmonster Data Compressor is considered deprecated, so the approach described below is not recommended for connecting to SQL databases.
To see the relevant way of connecting to relational databases, refer to this article.

Requirements

  • Flexmonster Pivot version 2.213 or higher
  • Microsoft .NET Core
  • A driver for the database

Supported databases

  • MySQL
  • Microsoft SQL Server - already has a built-in driver
  • PostgreSQL
  • Other ADO.NET databases

Step 1: Embed the component into your webpage

If Flexmonster is not yet embedded, set up an empty component in your webpage:

In pure JavaScript

Complete the Integrating Flexmonster guide. Your code should look similar to the following example:

let pivot = new Flexmonster({
  container: "pivotContainer",
  componentFolder: "node_modules/flexmonster/",
  toolbar: true
});

In React

Complete the Integration with React guide. Your code should look similar to the following example:

<FlexmonsterReact.Pivot
 toolbar={true}
/>

In Angular

Complete the Integration with Angular guide. Your code should look similar to the following example:

<fm-pivot
 [toolbar]="true">
</fm-pivot>

In Vue

Complete the Integration with Vue guide. Your code should look similar to the following example:

<Pivot
 toolbar
/>

Step 2: Setup the Data Compressor on the server

Install Flexmonster Data Compressor. Download it on nuget.org or run the following command in the Package Manager Console:

Install-Package Flexmonster.Compressor

Then add the appropriate database driver to your project.
Below is a connection and query sample for MySql:

string connetionString = "server=localhost;database=XXX;";
string sql = "SELECT * FROM YYY";
MySqlConnection sqlConnection = new MySqlConnection(connetionString);
sqlConnection.Open();
MySqlCommand command = new MySqlCommand(sql, sqlConnection);
DbDataReader dataReader = command.ExecuteReader();

The following line of code will convert DbDataReader to a Stream:

Stream inputStream = Compressor.CompressDb(dataReader);

Now, you can create a response from the Stream. For simple cases, it is suitable to read all content at once:

StreamReader reader = new StreamReader(inputStream);
string output = reader.ReadToEnd();

Step 3: Enable cross-origin resource sharing (CORS)

By default, the browser prevents JavaScript from making requests across domain boundaries. CORS allows web applications to make cross-domain requests. Here is a useful link explaining how to setup CORS on your server:

Step 4: Configure the report with your own data

Now it’s time to configure the pivot table on the webpage. Let’s create a minimal report for this (replace filename and other parameters with your specific values):

var pivot = new Flexmonster({
	container: "pivotContainer",
	toolbar: true,
	report: {
		dataSource: {
			type: "csv",
			/* URL to the Data Compressor .NET Core */
			filename: "http://localhost:55772/api/flexmonster/get"
		}
	},
	licenseKey: "XXXX-XXXX-XXXX-XXXX-XXXX"
});

Launch the webpage from a browser — there you go! A pivot table is embedded into your project.

Examples

Saving to a file

string connetionString = "server=localhost;database=XXX;";
string sql = "SELECT * FROM YYY";
MySqlConnection sqlConnection = new MySqlConnection(connetionString);
sqlConnection.Open();
MySqlCommand command = new MySqlCommand(sql, sqlConnection);
DbDataReader dataReader = command.ExecuteReader();

Stream inputStream = Compressor.CompressDb(dataReader);
FileStream fileStream = File.Create("data.csv");
inputStream.CopyTo(fileStream);
fileStream.Dispose();