Using SQL Data Sources
Our sample projects and report templates can help you learn the basics of working with our products.The connection string and SQL query of a report can be changed dynamically from code. This example swaps both when the report requests its data.
Preparing the data. Read or create the data source, register it in the report dictionary, synchronize the schema, and render with the supplied records.
The
Preparing the data. Read or create the data source, register it in the report dictionary, synchronize the schema, and render with the supplied records.
@Component({
selector: "using-sql-datasources",
imports: [Viewer],
template: `<sti-viewer [report]="report"></sti-viewer>`
})
export class UsingSqlDataSources {
report: Stimulsoft.Report.StiReport;
constructor() {
Stimulsoft.StiOptions.WebServer.url = "http://localhost:9615/";
this.report = new Stimulsoft.Report.StiReport();
this.report.loadFile("Reports/SimpleListSQL.mrt");
this.report.onBeginProcessData = function (args) {
// In this event, it is possible to handle the data request, and replace the connection parameters
// You can change the connection string
// This example uses the 'Northwind' SQL database, it is located in the 'Data' folder
// You need to import it and correct the connection string to your database
if (args.connection === 'MySQL')
args.connectionString = 'Server=localhost; Database=northwind; UserId=root; Pwd=;';
// You can change the SQL query
if (args.dataSource === 'customers')
args.queryString = 'SELECT * FROM MyTable';
}
}
}The
onBeginProcessData event exposes args.connectionString and args.queryString; replacing them per connection or data source lets one template target different databases. Requests are proxied through StiOptions.WebServer.url.