我正在创建一个包含多个.mdf的应用程序 用户可以在不同的数据库中导航在我的应用程序中更改连接工作正常。 但是当我尝试在Crystal Report中使用我的连接时,出现错误ReportError
编辑:有没有办法没有运行sp_attach_db
public static void Show(ReportDocument reportDocument, string reportFileName)
{
try
{
ReportViewer reportViewer = new ReportViewer();
CrystalDecisions.CrystalReports.Engine.ReportObjects crReportObjects;
CrystalDecisions.CrystalReports.Engine.SubreportObject crSubreportObject;
CrystalDecisions.CrystalReports.Engine.ReportDocument crSubreportDocument;
CrystalDecisions.CrystalReports.Engine.Database crDatabase;
CrystalDecisions.CrystalReports.Engine.Tables crTables;
TableLogOnInfo crTableLogOnInfo;
CrystalDecisions.Shared.ConnectionInfo crConnectioninfo = new CrystalDecisions.Shared.ConnectionInfo();
crConnectioninfo.ServerName = @".\SQLExpress";
crConnectioninfo.DatabaseName = @"C:\Database\MyDatabase.mdf";
crConnectioninfo.IntegratedSecurity = true;
crDatabase = reportDocument.Database;
crTables = crDatabase.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)
{
crTableLogOnInfo = crTable.LogOnInfo;
crTableLogOnInfo.ConnectionInfo = crConnectioninfo;
crTable.ApplyLogOnInfo(crTableLogOnInfo);
}
CrystalDecisions.CrystalReports.Engine.Sections crSections = reportDocument.ReportDefinition.Sections;
foreach (CrystalDecisions.CrystalReports.Engine.Section crSection in crSections)
{
crReportObjects = crSection.ReportObjects;
foreach (CrystalDecisions.CrystalReports.Engine.ReportObject crReportObject in crReportObjects)
{
if (crReportObject.Kind == ReportObjectKind.SubreportObject)
{
crSubreportObject = (CrystalDecisions.CrystalReports.Engine.SubreportObject)crReportObject;
crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);
crDatabase = crSubreportDocument.Database;
crTables = crDatabase.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)
{
crConnectioninfo.ServerName = @".\SQLExpress";
crConnectioninfo.DatabaseName = @"C:\Database\MyDatabase.mdf";
crConnectioninfo.IntegratedSecurity = true;
crTableLogOnInfo = crTable.LogOnInfo;
crTableLogOnInfo.ConnectionInfo = crConnectioninfo;
crTable.ApplyLogOnInfo(crTableLogOnInfo);
}
}
}
}
reportViewer.RptDocument = reportDocument;
reportViewer.RptDocument.ExportToDisk(ExportFormatType.PortableDocFormat, reportFileName);
reportViewer.ReportFileName = reportFileName;
reportViewer.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error in Loading Report", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}