Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Overview
The Crystal Reports Report Designer Component (RDC) has a number of
properties and methods available at runtime to connect a report to its data
source. This document provides an in depth look at each of these properties,
methods and their arguments. Developers can expect a better understanding of
how to connect Crystal Reports to their data sources at runtime.
Contents
INTRODUCTION ............................................................................................2
ACTIVE DATA SOURCES ..............................................................................2
SetDataSource and SetTablePrivateData ................................................... 2
Data Environments...................................................................................... 4
AddADOCommand and AddOLEDBSource................................................ 5
PERSISTENT DATA SOURCES .......................................................................7
SetLogonInfo ............................................................................................... 7
Location Property ....................................................................................... 9
SetSessionInfo Method .............................................................................. 10
LogonServer and LogonServerEx.............................................................. 10
CONTACTING CRYSTAL DECISIONS FOR TECHNICAL SUPPORT ....................12
6/24/2002 2:58 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 1
Crystal Reports Connecting the RDC to my data source
Introduction
Using the RDC, Crystal Reports is able to report off of both active data sources
and persistent data sources. Active data sources are recordsets, resultsets, data
arrays and Data Environments that only exist in memory while the application is
running. Persistent data sources are databases, tables, log files, directory
structures, etc. that reside and exist outside of the application. Persistent data can
be broken down further into PC-type data sources and server-type data sources.
The methods/properties of the RDC used to connect reports to these varying data
sources include:
Active Data:
SetDataSource, SetTablePrivateData
AddADOCommand
AddOLEDBSource
Persistent Data:
SetLogonInfo
Location, SetTableLocation
SetSessionInfo
LogonServer, LogonServerEx
6/24/2002 2:58 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 2
cr_rdc_dbconnectivity.pdf
Crystal Reports Connecting the RDC to my data source
The first argument, DataTag, is always passed in as the value 3 (this refers to
Active Data). The second argument, data, is one of the following:
DatabaseTable Object
Sub SetDataSource(data, [dataTag])
The first argument, data, is the recordset/resultset (as mentioned above). The
second optional argument, dataTag, is optional and should always be 3 when
used. Using any other value for dataTag will cause unexpected results. The
third optional argument, tableNumber, specifies which table in the report to
pass the recordset to (the numbering is 1-based with the first table in the report
having the value 1, the second table having the value 2, and so on).
6/24/2002 2:58 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 3
cr_rdc_dbconnectivity.pdf
Crystal Reports Connecting the RDC to my data source
Reports designed against an active data source should be designed to expect one
recordset. Linking between multiple recordsets defeats the purpose of active
data. A properly designed recordset will perform all of the linking and filtering
prior to passing the data to the Crystal Reports Active Data driver, p2smon.dll.
NOTE This document does not discuss how to create recordsets or how to design reports to use
Active Data. For more information on designing reports using active data, refer to the file:
scr8_ttxado.pdf
at http://support.crystaldecisions.com/library
Data Environments
The most important thing to remember when working with a Data Environment
is that a Data Environment is just a graphical interface for ADO recordsets.
When designing a report against a Data Environment, Crystal Reports needs a
method to save the structure of a recordset generated from the Data
Environment. Crystal Reports needs to know the order of the fields and their
data types to allow a report to be designed against the Data Environment.
Crystal Reports Active Data driver creates this structure internally by saving the
Data Environment’s (or ADO’s) Command object. However when an
application fails to properly pass a recordset to the report at runtime, unexpected
results can occur. This includes error messages such as “Server not yet open” or
“Physical Database not found” when the report is distributed, the database is
moved, or the Data Environment is changed.
The report is designed to only read the Command object once (at the original
design of the report) to retrieve the recordset’s structure. Any changes to the
Command object or Data Environment are not actively updated.
6/24/2002 2:58 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 4
cr_rdc_dbconnectivity.pdf
Crystal Reports Connecting the RDC to my data source
Report.Database.SetDataSource _
DataEnvironment1.Recordsets(1)
Notice that the code is identical to populating a recordset and using the
SetDataSource method.
NOTE If the report’s data structure is static (field names and field data types do not change; only
the volume of data changes from run to run of the report) then the report’s tables should
be added at design time using a Data Definition (TTX) file, and a recordset passed to the
report at runtime using the SetDataSource method.
6/24/2002 2:58 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 5
cr_rdc_dbconnectivity.pdf
Crystal Reports Connecting the RDC to my data source
ADOcn.CursorLocation = adUseClient
ADOcn.Open "DSN=Xtreme Sample Database"
ADOcmd.ActiveConnection = ADOcn
ADOcmd.CommandText = "Select * from Customer"
Report.Database.AddOLEDBSource _
"DSN=Xtreme Sample Database”, “Customer”
NOTE For more information on how to create an OLE DB connection string, see Microsoft
Knowledge Base article Q218600 at http://msdn.microsoft.com
6/24/2002 2:58 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 6
cr_rdc_dbconnectivity.pdf
Crystal Reports Connecting the RDC to my data source
SetLogonInfo
Location, SetTableLocation
SetSessionInfo
LogonServer, LogonServerEx
SetLogonInfo
The SetLogonInfo method of the DatabaseTable object is the most popular
method for connecting a report to its database. SetLogonInfo is primarily used
for connecting to ODBC and server-type databases. Server-type databases
include MS SQL Server, Oracle, DB2, Informix, Sybase, Pervasive SQL Server,
etc.
TIP To use the logon values already stored in a report (RPT) file, use empty strings (“”) for the
pServerName, pDatabaseName, and pUserID arguments. You will always need to set a
value for the pPassword.
6/24/2002 2:58 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 7
cr_rdc_dbconnectivity.pdf
Crystal Reports Connecting the RDC to my data source
NOTE When connecting through OLE DB to an MS Jet provider, pServerName is the path to the
Access Database (such as “c:\path\to\database.mdb”).
When using an Oracle database, pDatabaseName should be set to an empty string (“”).
NOTE When using ODBC, pServerName sets the data source location to a new ODBC DSN.
PServerName cannot be used to specify a different path/location than what is specified in
the ODBC DSN.
To change the path to a PC-type data source when using a single ODBC DSN, pass in a
database qualifier as the value for PdatabaseName using the syntax:
"<CRWDC>DBQ=<path to the new database>"
6/24/2002 2:58 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 8
cr_rdc_dbconnectivity.pdf
Crystal Reports Connecting the RDC to my data source
Tip To see the pServerName, pDatabaseName and pUserid values that your report is
designed against, check the Set Location box under the Database | Set Location menu
in Crystal Reports.
This example shows an ODBC connection to a MS Access database:
Location Property
The Location property of the DatabaseTable object is used to change tables
when connecting to a server type-database. The Location property also sets the
path to a PC-type database.
6/24/2002 2:58 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 9
cr_rdc_dbconnectivity.pdf
Crystal Reports Connecting the RDC to my data source
SetSessionInfo Method
The SetSessionInfo method of the DatabaseTableObject is used when
connecting natively to a MS Access database that contains either session level
(user level) security, database level security, or both.
NOTE When connecting through OLE DB to a secure MS Access database use LogonServerEx
6/24/2002 2:58 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 10
cr_rdc_dbconnectivity.pdf
Crystal Reports Connecting the RDC to my data source
pDllName is the Crystal Reports database driver used in the report (such as
P2sodbc.dll). This can be checked under the Database | Convert Database Driver
menu in Crystal Reports.
Notice that the LogonServerEx arguments are exactly the same as those for
LogonServer, with the addition of pServerType and pConnectionString. The
pServerType argument value should be identical to the Server Type field found
in the Set Location box in Crystal Reports. The pConnectionString argument
value is used for setting OLE DB connection strings. This allows more
flexibility when using OLE DB as a connection string can be used in place of
the separate pDatabaseName, pUserID, pPassword arguments.
6/24/2002 2:58 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 11
cr_rdc_dbconnectivity.pdf
Crystal Reports Connecting the RDC to my data source
Self-serve Support:
http://support.crystaldecisions.com/
Email Support:
http://support.crystaldecisions.com/support/answers.asp
Telephone Support:
http://www.crystaldecisions.com/contact/support.asp
6/24/2002 2:58 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 12
cr_rdc_dbconnectivity.pdf