Using a database as a data source is one of the most common scenarios when you work with reports. This topic explains how to bind your ActiveReports report to a SQL, OLEDB or ODBC data source.
The example below uses a SQL database as a data source.
Copy Code
|
|
---|---|
private PageReport LoadReport() { string path = ""; // Your path to the report PageReport report = new PageReport(new FileInfo(path)); report.Document.LocateCredentials += OnLocateCredentials; string dataProviderName = "SQL"; // Or use "OLEDB" or "ODBC" if you need it DataSource dataSource = report.Report.DataSources[0]; // Your data source if(dataSource.ConnectionProperties.DataProvider == dataProviderName) { dataSource.ConnectionProperties.ConnectString = ConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString; } return report; } private void OnLocateCredentials(object sender, LocateCredentialsEventArgs args) { args.Password = ""; // Your password if you need it args.UserName = ""; // Your username if you need it } |
Use the following strings to connect to different data providers:
Copy Code
|
|
---|---|
Data Source=[sourceName];Initial Catalog=[dbName];Integrated Security=True;Connect Timeout=30;Encrypt=False; |
Copy Code
|
|
---|---|
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=[dbname]; |
Copy Code
|
|
---|---|
DRIVER=SQLite3 ODBC Driver;Database=[dbname]; LongNames=0; Timeout=1000; NoTXN=0; SyncPragma=NORMAL; StepAPI=0; |
When working with a data provider, you may need to process user credentials to work with the target source. The LocateCredentials event is used for this purpose.
Copy Code
|
|
---|---|
report.Document.LocateCredentials += OnLocateCredentials; |
Assign a handler that specifies the password and username when they are requested.
Copy Code
|
|
---|---|
private void OnLocateCredentials(object sender, LocateCredentialsEventArgs args) { args.Password = ""; // Your password if you need it args.UserName = ""; // Your username if you need it } |
The event handler receives an argument of the LocateCredentialsEventArgs type that contains data, related to this event. The following LocateCredentialsEventArgs properties provide information, specific to this event.
Note: We assume that the report has defined data fields and datasets, which can be done in various ways, e.g., in the Web or Windows Forms Designer.