我正在编写一些C#代码以打开Crystal Report并运行它。报表本身是针对Dev db设计的,因此在内部将dev db服务器名称存储在其中。 Prod数据库服务器具有与dev相同的所有功能。在C#中,我打开代码,使用SetDatabaseLogon()更改登录数据,然后尝试导出到流。
如果数据库服务器名称与针对此名称开发的名称相同,则一切正常。但是,当我将数据库服务器名称更改为生产时,我得到ExportToStream()给我通用的“对象引用未设置为对象的实例”。关于在其他所有条件不变的情况下如何切换数据库服务器的任何想法?
public Stream GetReportStream(string reportName, string exportType, IEnumerable<KeyValuePair<string, string>> rptParams)
{
ReportClass rpt = new ReportClass();
rpt.FileName = Path.Combine(@"P:\Temp\", reportName + ".rpt");
rpt.Load();
var connections = rpt.DataSourceConnections;
for (var i = 0; i < connections.Count; i++)
{
//rpt.SetDatabaseLogon(connections[i].UserID, "MYPWHERE", connections[i].ServerName, connections[i].DatabaseName); // this sets the info to whatever was already in the db which is the dev server and that works
rpt.SetDatabaseLogon(connections[i].UserID, "MYPWHERE", "PRODDBSERVER", connections[i].DatabaseName);
}
// figure out what type each parameter is and then set it
foreach (var param in rptParams)
{
// is int?
int intParam = 0;
if (int.TryParse(param.Value, out intParam))
{
rpt.SetParameterValue("@" + param.Key, intParam);
continue;
}
// is date?
DateTime dateParam;
if (DateTime.TryParse(param.Value, out dateParam))
{
rpt.SetParameterValue("@" + param.Key, dateParam);
continue;
}
}
// return the report to web client
return rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
}