Para cambiar el servidor, base de datos, user id y password de reportes crystal en tiempo de ejecucion (runtime) se debe indicar la informacion de la conexion a cada tabla del reporte y tablas de los subreportes.
private bool ApplyLogon(ReportDocument cr, ConnectionInfo ci)
{
TableLogOnInfo li;
// for each table apply connection info
foreach (CrystalDecisions.CrystalReports.Engine.Table tbl in cr.Database.Tables)
{
li = tbl.LogOnInfo;
li.ConnectionInfo = ci;
tbl.ApplyLogOnInfo(li);
// check if logon was successful
// if TestConnectivity returns false, check
// logon credentials
if (tbl.TestConnectivity())
{
// drop fully qualified table location
if (tbl.Location.IndexOf(".") > 0)
tbl.Location = tbl.Location.Substring(tbl.Location.LastIndexOf(".") + 1);
else
tbl.Location = tbl.Location;
}
else
return false;
}
return true;
}
// El metodo recorre todas las tablas
private bool Logon(ReportDocument cr, string server, string db, string id, string pass)
{
ConnectionInfo ci = new ConnectionInfo();
SubreportObject subObj;
ci.ServerName = server;
ci.DatabaseName = db;
ci.UserID = id;
ci.Password = pass;
if (!ApplyLogon(cr, ci))
return false;
foreach (ReportObject obj in cr.ReportDefinition.ReportObjects)
{
if (obj.Kind == ReportObjectKind.SubreportObject)
{
subObj = (SubreportObject)obj;
if (!ApplyLogon(cr.OpenSubreport(subObj.SubreportName), ci))
return false;
}
}
return true;
}
private bool ApplyLogon(ReportDocument cr, ConnectionInfo ci)
{
TableLogOnInfo li;
// for each table apply connection info
foreach (CrystalDecisions.CrystalReports.Engine.Table tbl in cr.Database.Tables)
{
li = tbl.LogOnInfo;
li.ConnectionInfo = ci;
tbl.ApplyLogOnInfo(li);
// check if logon was successful
// if TestConnectivity returns false, check
// logon credentials
if (tbl.TestConnectivity())
{
// drop fully qualified table location
if (tbl.Location.IndexOf(".") > 0)
tbl.Location = tbl.Location.Substring(tbl.Location.LastIndexOf(".") + 1);
else
tbl.Location = tbl.Location;
}
else
return false;
}
return true;
}
// El metodo recorre todas las tablas
private bool Logon(ReportDocument cr, string server, string db, string id, string pass)
{
ConnectionInfo ci = new ConnectionInfo();
SubreportObject subObj;
ci.ServerName = server;
ci.DatabaseName = db;
ci.UserID = id;
ci.Password = pass;
if (!ApplyLogon(cr, ci))
return false;
foreach (ReportObject obj in cr.ReportDefinition.ReportObjects)
{
if (obj.Kind == ReportObjectKind.SubreportObject)
{
subObj = (SubreportObject)obj;
if (!ApplyLogon(cr.OpenSubreport(subObj.SubreportName), ci))
return false;
}
}
return true;
}
Gracias por tu ayuda, increiblemente en el 2010, me sirvió esto. Al menos fue la primer solución que encontré y funcionó a la primera.
ResponderBorrar