Using custom data provider in Report Sharp-Shooter

Vitaliy Korney

Want your own data provider be available in the Designer?

Let’s review how it’s possible on the example of mysql.

In order to get it work you need to perform the following steps:

First of all, we need to know how it works. This feature uses Reflection in order to create a list of available data sources. During design time, current assembly is not loaded, so we have to take care about putting our own DataSource implementation in the separate library.

Let’s add a new project to our solution and name it “DataSourceLibrary” and add the following references:

MySql.Data

PerpetuumSoft.Reporting

Now it’s time to implement abstract class from PerpetuumSoft.Reporting.Data namespace

public class MySQLDataSource : SqlBaseDataSource

{

public override void FillDataTable(System.Data.DataTable table)

{

using (MySql.Data.MySqlClient.MySqlDataAdapter adapter = new MySql.Data.MySqlClient.MySqlDataAdapter(this.SelectQuery, this.ConnectionString))

{

adapter.Fill(table);

}

}

public override object Clone()

{

MySQLDataSource copy = new MySQLDataSource();

copy.Name = Name;

copy.ConnectionString = ConnectionString;

copy.SelectQuery = SelectQuery;

return copy;

}

}

You may think that’s it. Not yet, nothing makes your assembly loaded. You need to create a component in this library and put it on your form or in the service designer.

Let’s name this component MySQLDataSourceSupport.

MySQL Data Source

Then open the designer and DataSources dialog:

DataSources dialog

We can see that this data provider is available, let’s just add and configure it appropriately:

data provider

Connection string:

server=<serverIP or Name>;User Id=<userName>;password=<password>;database=<databaseName>

Name:

DataSource1.

SelectQuery:

For example: SELECT `id`, `var1`, `var2` FROM `test`

Use your data source in your report:

data source in report

We have reviewed how it is possible to extend the list of available data providers with your own data provider. Now you can implement your own SqlDataSource and use it in your report.

August 17th, 2011

Comments

  1. Andrew Kazyrevich:

    Vitaliy, that’s fabulous! Looking forward to seeing a data provider for MongoDB :)

Leave a Comment