Despite the COVID-19 outbreak, our team continues operating at full speed. We are always here to support and answer all your questions.

Feel free to reach out by filling this quick form.

Fill the form
Get Free Trial

Connecting to a relational database with .NET

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 Framework 4 or higher
  • A driver for the database

Supported databases

  • Oracle Database – driver
  • MySQL – driver
  • Microsoft SQL Server – already has a built-in driver
  • PostgreSQL – driver
  • Other ADO.NET / ODBC databases

Step 1: Embed the component into your web page

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

In pure JavaScript

Complete the Quick start guide. Your code should look similar to the following example:

var pivot = new Flexmonster({
    container: "pivotContainer",
    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 React

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

<FlexmonsterReact.Pivot
toolbar={true}
/>

In Vue

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

<Pivot
ref="pivot"
toolbar>
</Pivot>

Step 2: Setup Flexmonster Data Compressor on the server

Add the following dependencies to your project:

  • An appropriate database driver. Note that Microsoft SQL Server already has a built-in driver.
  • Flexmonster.Compressor.dll – located in the Pivot Table for Databases/server/.net/ folder of the download package.

Below is a connection and query sample for MS SQL Server:

string connectionString = "Server=localhost;Database=XXX;";
SqlConnection sqlConnection = new SqlConnection(connectionString);
sqlConnection.Open();
string query = "SELECT * FROM YYY";
DbCommand command = new SqlCommand(query, sqlConnection);
DbDataReader reader = command.ExecuteReader();

Then the following line of code will convert DbDataReader to a Stream:

Stream inputStream = Flexmonster.Compressor.CompressDb(reader);

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

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

It is also possible to create a streaming response. This means that the end user will get the response with minimal delay and server will use less memory. This is the recommended approach for large datasets:

HttpResponseMessage response = Request.CreateResponse();
response.Content =
    new PushStreamContent((Stream outputStream, HttpContent content, 
                           TransportContext context) =>
    {
        int length = 0;
        byte[] buffer = new byte[10240];
        while ((count = inputStream.Read(buffer, 0, buffer.Length)) > 0)
        {
            outputStream.Write(buffer, 0, length);
            outputStream.Flush();
        }
        outputStream.Close();
    }, new MediaTypeHeaderValue("text/plain")
);

The full project is available at Pivot Table for Databases/server/.net/ inside the download package.

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 web page. 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 */
			filename: "http://localhost:55772/api/flexmonster/get"
		}
	},
	licenseKey: "XXXX-XXXX-XXXX-XXXX-XXXX"
});

Launch the web page from a browser — there you go! A pivot table is embedded into your project. Check out the example on JSFiddle.

Examples

Saving to a file

string connectionString = "Server=localhost;Database=XXX;";
SqlConnection sqlConnection = new SqlConnection(connectionString);
sqlConnection.Open();
string query = "SELECT * FROM YYY";
DbCommand command = new SqlCommand(query, sqlConnection);
DbDataReader reader = command.ExecuteReader();

Stream inputStream = Flexmonster.Compressor.CompressDb(reader);
FileStream fileStream = File.Create("data.csv");
inputStream.CopyTo(fileStream);
fileStream.Close();

What’s next?