connexion à la base de données à Crystal Report

Transmettre dynamiquement les informations de connexion à la base de données à Crystal Reports dans VB.NET et C#.NET

Partagez sur:

Dans ce tutoriel nous allons voir comment transmettre dynamiquement les informations de connexion à la base de données à Crystal Reports dans VB.net et C#.

Crystal Reports est un outil qui vous permet de créer des rapports pour des applications, il est beaucoup utilisé par les développeurs DotNet.

Dans de nombreuses situations, par exemple si vous développez un projet de base de données sur un serveur de test et que vous devez ensuite le migrer ou lorsque vous installez votre application sur des postes clients, dans de telles situations, vous devez fournir les informations de connexion à la base de données à Crystal Reports de façons dynamique.

Lorsque vous êtes nouveau dans VB.Net Crystal Reports, vous recevez souvent des messages d’echec de l’ouverture de la connexion lorsque vous essayer d’ouvrir un rapport.

Il s’agit d’un problème courant avec l’intégration de Crystal Reports dans VB.Net.

Le passage de paramètre d’ouverture de session dynamique résoudra ce problème.

Transmettre dynamiquement les informations de connexion à la base de données à Crystal Reports dans VB.NET et C#.NET

Nous allons voir comment comment transmettre dynamiquement les informations de connexion à la base de données à Crystal Reports au moment de l’exécution.

Lire aussi: Comment créer une zone de texte numérique en C# et VB.net ?

Ici, nous transmettons dynamiquement le nom du serveur, l’ID utilisateur et le mot de passe à Crystal Reports.

Sélectionnez le formulaire par défaut (Form1.vb) que vous avez créé dans VB.NET et faites glisser un bouton et un contrôle CrystalReportViewer dans votre formulaire.

Vous pouvez mettre le code source suivant dans l’événement de clic d’un bouton ou enlèvement «Load» du formulaire qui contient le contrôle CrystalReportViewer.

Code source en VB.net

Imports CrystalDecisions.CrystalReports.Engine

Imports CrystalDecisions.Shared

Dim crtableLogoninfos As New TableLogOnInfos()

Dim crtableLogoninfo As New TableLogOnInfo()

Dim crConnectionInfo As New ConnectionInfo()

Dim CrTables As Tables

Dim CrTable As Table

Dim TableCounter

'If you are using a Strongly Typed report (Imported in 'your project) named CrystalReport1.rpt use the 'following:

Dim crReportDocument As New CrystalReport1()

'If you are using a Non-Typed report, and loading a report outside of the project, use the following:

Dim crReportDocument As New ReportDocument()

crReportDocument.Load("c:\myReports\myReport.rpt")

'Set the ConnectionInfo properties for logging on to the Database If you are using ODBC, this should be the DSN name NOT the physical server name. If you are NOT using ODBC, this should be the physical server name 

With crConnectionInfo

.ServerName = "DSN or Server Name"

'If you are connecting to Oracle there is no 'DatabaseName. Use an empty string.

'For example,

.DatabaseName = ""

.DatabaseName = "DatabaseName"

.UserID = "Your User ID"

.Password = "Your Password"

End With

'This code works for both user tables and stored procedures. Set the CrTables to the Tables collection of the report  

CrTables = crReportDocument.Database.Tables

'Loop through each table in the report and apply the LogonInfo information

For Each CrTable in CrTables

CrTableLogonInfo = CrTable.LogonInfo

CrTableLogonInfo.ConnectionInfo = crConnectionInfo

CrTable.ApplyLogOnInfo(crtableLogoninfo)

'If your DatabaseName is changing at runtime, specify the table location.

'For example, when you are reporting off of a Northwind database on SQL server you should have the following line of code:

crTable.Location = "Northwind.dbo." & crTable.Location.Substring(crTable.Location.LastIndexOf(".") + 1)

Next

'Set the viewer to the report object to be previewed.

CrystalReportViewer1.ReportSource = crReportDocument

Code source C#

using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

private CrystalReport1 crReportDocument = new

CrystalReport1 ();

private Database crDatabase;

private Tables crTables;

private Table crTable;

private TableLogOnInfo crTableLogOnInfo;

private ConnectionInfo crConnectionInfo = new ConnectionInfo ();

//Setup the connection information structure to log on to the data source for the report.

// If using ODBC, this should be the DSN. If using OLEDB, etc, this should be the physical server name

crConnectionInfo.ServerName = "DSN or Server Name";

// If you are connecting to Oracle there is no DatabaseName. Use an empty string i.e. crConnectionInfo.DatabaseName = "";

crConnectionInfo.DatabaseName = "DatabaseName";

crConnectionInfo.UserID = "Your UserID";

crConnectionInfo.Password = "Your Password";

// This code works for both user tables and stored procedures. Get the table information from the report

crDatabase = crReportDocument.Database;

crTables = crDatabase.Tables;

//Loop through all tables in the report and apply the connection information for each table.

for (int i = 0; i < crTables.Count; i++)

{

crTable = crTables [i];

crTableLogOnInfo = crTable.LogOnInfo;

crTableLogOnInfo.ConnectionInfo = crConnectionInfo;

crTable.ApplyLogOnInfo(crTableLogOnInfo);

//If your DatabaseName is changing at runtime, specify the table location. For example, when you are reporting off of a Northwind database on SQL server you should have the following line of code:

crTable.Location = "Northwind.dbo." + crTable.Location.Substring(crTable.Location.LastIndexOf(".") + 1)

}

//Set the viewer to the report object to be previewed.

crystalReportViewer1.ReportSource = crReportDocument;

Conclusion

Dans tutoriel, nous avons vu comment transmettre dynamiquement les informations de connexion à la base de données à Crystal Reports dans VB.net et C# .


Partagez sur: