Data Connectivity
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Data Connectivity
Message Icon Topic: Database Neutral Report Post Reply Post New Topic
Author Message
cobill
Newbie
Newbie


Joined: 17 Oct 2008
Location: United States
Online Status: Offline
Posts: 11
Quote cobill Replybullet Topic: Database Neutral Report
    Posted: 11 May 2009 at 10:43am
I have a report that works fine, but I use the same report on multiple servers/databases.  In order to get this to work, I have several versions of the same report file, one for each of the databases "hardcoded" via the CR2008 GUI when I created the original report, saved after a select datasource change for each database.  The database is identical for each of the different servers (except for the data).  The .rpt file is called by a C# program that currently passes in an input parameter, but no connection info.  Is passing a connection string in C# the best way to do this?  I've tried an ODBC connection on the servers with a dns file identical to one on my C drive, but this seems just another way to hardcode it.  Is there a way to make a database neutral .rpt file that will run on any compatible database without hardcoding it with a .dns file or a select datasource change? 
cobill
IP IP Logged
hilfy
Admin Group
Admin Group
Avatar

Joined: 20 Nov 2006
Online Status: Offline
Posts: 3702
Quote hilfy Replybullet Posted: 12 May 2009 at 12:19pm
Is it the same database type just a different db location for each report?  If so, this is not difficult.
 
I assume that your program knows which database to connect the report to.  Using the ReportDocument object model, here's the code we use for this:
ConnectionInfo connectionInfo = new CrystalDecisions.Shared.ConnectionInfo();
connectionInfo.ServerName = myDB;
connectionInfo.UserID = myUser;
connectionInfo.Password = myPassword;
 
// set report connection for main report
SetDBLogonForReport(connectionInfo, crReport, qServer);
// set report connection for any subreports
SetDBLogonForSubreports(connectionInfo, crReport, qServer);
---------------------
private void SetDBLogonForReport(ConnectionInfo connectionInfo, 
    ReportDocument reportDocument, string qServer)
{
  Tables tables = reportDocument.Database.Tables;
  foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
  {
    TableLogOnInfo tableLogonInfo = table.LogOnInfo;
    tableLogonInfo.ConnectionInfo = connectionInfo;
    table.ApplyLogOnInfo(tableLogonInfo);
  }
}
---------------------
private void SetDBLogonForSubreports(ConnectionInfo connectionInfo,
    ReportDocument reportDocument, string qServer)
{
  Sections sections = reportDocument.ReportDefinition.Sections;
  foreach (Section section in sections)
  {
    ReportObjects reportObjects = section.ReportObjects;
    foreach (ReportObject reportObject in reportObjects)
    {
      if (reportObject.Kind == ReportObjectKind.SubreportObject)
      {
        SubreportObject subreportObject = (SubreportObject)reportObject;
        ReportDocument subReportDocument = 
           subreportObject.OpenSubreport(subreportObject.SubreportName);
        SetDBLogonForReport(connectionInfo, subReportDocument, qServer);
      }
    }
  }
}
 
This only works for reports that are connecting to the same type of database as the one the report was designed with - all SQL Server, all Oracle, etc.  You can't run one time with SQL Server and another on Oracle using this code.
 
-Dell
IP IP Logged
cobill
Newbie
Newbie


Joined: 17 Oct 2008
Location: United States
Online Status: Offline
Posts: 11
Quote cobill Replybullet Posted: 13 May 2009 at 7:59am
Thanks!  I think this is exactly what I've been looking for.  One more question - My report uses a SQL Query command rather than mapping individual tables.  Can I stop at the server, database level or should I include the tables I'm referencing in the command query?  Thanks.
cobill
IP IP Logged
hilfy
Admin Group
Admin Group
Avatar

Joined: 20 Nov 2006
Online Status: Offline
Posts: 3702
Quote hilfy Replybullet Posted: 13 May 2009 at 9:40am
I'm not sure - we don't use any commands in our "View on Demand" reports, they're only in a couple of reports that are scheduled in Business Objects Enterprise.  So, I haven't done any testing in that direction.
 
-Dell
IP IP Logged
Post Reply Post New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum



This page was generated in 0.031 seconds.