0
我正在WPF中开发一个应用程序,它需要从中获取报告,我完成并在我的电脑中一切正常,没有数据库登录对话框出现,并且完全没问题。但是,当我想通过Crystal Report Viewer显示报表时,在客户端PC上发布数据库登录对话框时!我做了所有想到的事情,并在网络上到处搜索,尽我所能,但无法解决这个问题。 这是我的代码:Crystal Report在发布数据库时请求数据库登录
ReportDocument CryRpt1 = new ReportDocument();
string Path1 = System.Windows.Forms.Application.StartupPath + "\\CryReportSendItems.rpt";
CryRpt1.Load(Path1, OpenReportMethod.OpenReportByDefault);
AssignConnection(CryRpt1);
CryRpt1.Refresh();
Crv.ViewerCore.ReportSource = CryRpt1;
这是AssignConnection
方法:
private void AssignConnection(ReportDocument rpt)
{
ConnectionInfo connection = new ConnectionInfo();
connection.ServerName = "*ServerName*";
connection.DatabaseName = "*DBName*";
connection.UserID = "*User*";
connection.Password = "*Password*";
foreach (CrystalDecisions.CrystalReports.Engine.Table table in rpt.Database.Tables)
{
AssignTableConnection(table, connection);
}
foreach (CrystalDecisions.CrystalReports.Engine.Section section in rpt.ReportDefinition.Sections)
{
foreach (CrystalDecisions.CrystalReports.Engine.ReportObject reportObject in section.ReportObjects)
{
if (reportObject.Kind == ReportObjectKind.SubreportObject)
{
SubreportObject subReport = (SubreportObject)reportObject;
ReportDocument subDocument = subReport.OpenSubreport(subReport.SubreportName);
foreach (CrystalDecisions.CrystalReports.Engine.Table table in subDocument.Database.Tables)
{
AssignTableConnection(table, connection);
}
subDocument.SetDatabaseLogon(connection.UserID, connection.Password, connection.ServerName, connection.DatabaseName);
}
}
}
rpt.SetDatabaseLogon(connection.UserID, connection.Password, connection.ServerName, connection.DatabaseName);
}
private void AssignTableConnection(CrystalDecisions.CrystalReports.Engine.Table table, ConnectionInfo connection)
{
// Cache the logon info block
TableLogOnInfo logOnInfo = table.LogOnInfo;
connection.Type = logOnInfo.ConnectionInfo.Type;
// Set the connection
logOnInfo.ConnectionInfo = connection;
// Apply the connection to the table!
table.LogOnInfo.ConnectionInfo.DatabaseName = connection.DatabaseName;
table.LogOnInfo.ConnectionInfo.ServerName = connection.ServerName;
table.LogOnInfo.ConnectionInfo.UserID = connection.UserID;
table.LogOnInfo.ConnectionInfo.Password = connection.Password;
table.LogOnInfo.ConnectionInfo.Type = connection.Type;
table.ApplyLogOnInfo(logOnInfo);
}
*注:我的Windows版本是10(64)和客户端的Windows版本是7(64位)。
有些人可以帮我吗?
是你的数据库在你的WINDOWS 7机器上?或其他机器? –
检查连接字符串也.. –
@reds是的,我们在网络上。我刚发现它,需要填写数据源。请阅读答案。 – Hamed