How can I change the data source (database server, username, password) that a Crystal report uses at runtime, running within a crystal server?
I have a crystal server and have uploaded reports that have a set datasource (SQL Server 2005 hosted on SERVER A, userA, passwordA). I would like to schedule reports to run using a different datasource (SQL Server 2005 hosted on SERVER B, userB, passwordB) from the c# client I've written.
The c# client can schedule reports to run within the server using objects provided by the crystal webservices. I've been using the following 3 objects:
BIPlatform
InfoObject
CrystalReport
Documentation on these objects can be found HERE
1/30/2018 -Just wanted to add that you will need the CrystalDecisions.ReportAppServer.DataDefModel dll
I had the same problem, where i have a report that has MSAccess database connection and i needed to change that to SQLServer, it took 2 days to figure this out:
reportDocument = new ReportDocument();
reportDocument.Load(reportFileName);
TableLogOnInfo tableLogOnInfo = ReportClass.GetSQLTableLogOnInfo(connectionProperties.DatabaseSource, connectionProperties.DatabaseName, connectionProperties.UserName, connectionProperties.Password);
for (int i = 0; i < reportDocument.Database.Tables.Count; i++)
{
Table table = reportDocument.Database.Tables[i];
table.ApplyLogOnInfo(tableLogOnInfo);
}
public static ConnectionInfo GetConnectionInfo(string serverName, string databaseName, string userID, string password)
{
ConnectionInfo connectionInfo = new ConnectionInfo();
connectionInfo.ServerName = serverName;
connectionInfo.DatabaseName = databaseName;
connectionInfo.UserID = userID;
connectionInfo.Password = password;
return connectionInfo;
}
public static TableLogOnInfo GetSQLTableLogOnInfo(string serverName, string databaseName, string userID, string password)
{
CrystalDecisions.ReportAppServer.DataDefModel.PropertyBag connectionAttributes = new CrystalDecisions.ReportAppServer.DataDefModel.PropertyBag();
connectionAttributes.EnsureCapacity(11);
connectionAttributes.Add("Connect Timeout", "15");
connectionAttributes.Add("Data Source", serverName);
connectionAttributes.Add("General Timeout", "0");
connectionAttributes.Add("Initial Catalog", databaseName);
connectionAttributes.Add("Integrated Security", false);
connectionAttributes.Add("Locale Identifier", "1033");
connectionAttributes.Add("OLE DB Services", "-5");
connectionAttributes.Add("Provider", "SQLOLEDB");
connectionAttributes.Add("Tag with column collation when possible", "0");
connectionAttributes.Add("Use DSN Default Properties", false);
connectionAttributes.Add("Use Encryption for Data", "0");
DbConnectionAttributes attributes = new DbConnectionAttributes();
attributes.Collection.Add(new NameValuePair2("Database DLL", "crdb_ado.dll"));
attributes.Collection.Add(new NameValuePair2("QE_DatabaseName", databaseName));
attributes.Collection.Add(new NameValuePair2("QE_DatabaseType", "OLE DB (ADO)"));
attributes.Collection.Add(new NameValuePair2("QE_LogonProperties", connectionAttributes));
attributes.Collection.Add(new NameValuePair2("QE_ServerDescription", serverName));
attributes.Collection.Add(new NameValuePair2("SSO Enabled", false));
ConnectionInfo connectionInfo = ReportClass.GetConnectionInfo(serverName, databaseName, userID, password);
connectionInfo.Attributes = attributes;
connectionInfo.Type = ConnectionInfoType.SQL;
TableLogOnInfo tableLogOnInfo = new TableLogOnInfo();
tableLogOnInfo.ConnectionInfo = connectionInfo;
return tableLogOnInfo;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With