使用远程sql server作为数据源在运行时创建水晶报告

时间:2016-01-13 13:51:14

标签: c# sql-server crystal-reports

我在同一台服务器上安装了我的数据库和应用程序(web,api和服务总线,所有这些都在运行时创建了水晶报告)。所有这些都在运行时完美地创建了报告。

将数据库移至另一台服务器,该服务器仅允许来自应用服务器的远程连接。

对于我的数据库连接,我在项目中所要做的就是将连接字符串中的服务器从(本地)更改为数据库服务器的ip地址,这一切都正常。

然而,似乎只是将服务器从(本地)更改为水晶报告的IP地址不起作用(提供"数据库登录失败"错误)

我不确定这是否是问题,但是为了在本地创建报告(在将.rpt上传到服务器之前),我必须创建一个连接,其中服务器设置为&#34; local&# 34; (在数据源位置)。由于我无法从本地计算机远程访问新数据库,因此无法更改<)>

我使用的代码如下:

string server = ConfigurationManager.AppSettings["Server"];
string database = ConfigurationManager.AppSettings["Database"];
string user = ConfigurationManager.AppSettings["DatabaseUser"];
string password = ConfigurationManager.AppSettings["DatabasePassword"];
var report = new ReportClass {FileName = reportPath};
report.Load();
report.SetDatabaseLogon(user, password, server, database);
var parameterValue = new ParameterDiscreteValue {Value = item.Id};
var parameter = new ParameterValues {parameterValue};
report.DataDefinition.ParameterFields["@id"].ApplyCurrentValues(parameter);
report.ExportToDisk(ExportFormatType.PortableDocFormat, path);

2 个答案:

答案 0 :(得分:0)

谢谢,我最终得到了这个(使用sql) 本质上,我们的想法是不仅为报告设置登录信息,还为所有子报告设置登录信息。如果一个人可以在报告中做一个&#34; foreach子报告,本来会很好。&#34; ,但似乎没有那样工作

        public static void SetConnections(ReportDocument report)
    {
        Database database = report.Database;

        Tables tables = database.Tables;

        var crConnInfo = new ConnectionInfo
        {
            ServerName = ConfigurationManager.AppSettings["Server"],
            DatabaseName = ConfigurationManager.AppSettings["Database"],
            UserID = ConfigurationManager.AppSettings["DatabaseUser"],
            Password = ConfigurationManager.AppSettings["DatabasePassword"]
        };

        foreach (Table table in tables)
        {
            TableLogOnInfo crLogOnInfo = table.LogOnInfo;
            crLogOnInfo.ConnectionInfo = crConnInfo;
            table.ApplyLogOnInfo(crLogOnInfo);
        }

        Sections crSections = report.ReportDefinition.Sections;
        foreach (Section crSection in crSections)
        {
            ReportObjects crReportObjects = crSection.ReportObjects;
            foreach (ReportObject crReportObject in crReportObjects)
            {
                if (crReportObject.Kind == ReportObjectKind.SubreportObject)
                {
                    var crSubreportObject = (SubreportObject)crReportObject;

                    ReportDocument subRepDoc = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);

                    Database crDatabase = subRepDoc.Database;
                    Tables crTables = crDatabase.Tables;

                    foreach (Table crTable in crTables)
                    {
                        TableLogOnInfo crLogOnInfo = crTable.LogOnInfo;
                        crLogOnInfo.ConnectionInfo = crConnInfo;
                        crTable.ApplyLogOnInfo(crLogOnInfo);
                    }
                }
            }
        }
    }

答案 1 :(得分:-1)

我在oracle数据库上使用Crystal报表,在我的应用程序中我使用此代码: CrystalDecisions.CrystalReports.Engine.ReportDocument rpt = new CrystalDecisions.CrystalReports.Engine.ReportDocument(); //reset connection DBController.LogonEx(DBAlias, "", DBUsername, DBPassword); //Create the QE (query engine) propertybag with the provider details and logon property bag CrystalDecisions.ReportAppServer.DataDefModel.PropertyBag QE_Details = new CrystalDecisions.ReportAppServer.DataDefModel.PropertyBag(); ConnectionInfo ci = DBController.GetConnectionInfos(null)[0]; //copy from existing attributes except for server name for (int idx = 0; idx < ci.Attributes.PropertyIDs.Count; idx++) { switch (ci.Attributes.PropertyIDs[idx]) { case "QE_ServerDescription": QE_Details.Add(ci.Attributes.PropertyIDs[idx], DBAlias); break; case "QE_LogonProperties": //this is itself a property bag CrystalDecisions.ReportAppServer.DataDefModel.PropertyBag logonDetails = new CrystalDecisions.ReportAppServer.DataDefModel.PropertyBag(); PropertyBag OLDLogon = (PropertyBag)ci.Attributes[ci.Attributes.PropertyIDs[idx]]; for (int idx2 = 0; idx2 < OLDLogon.PropertyIDs.Count; idx2++) { switch (OLDLogon.PropertyIDs[idx2]) { case "Server": case "Data Source": logonDetails.Add(OLDLogon.PropertyIDs[idx2], DBAlias); break; default: logonDetails.Add(OLDLogon.PropertyIDs[idx2], OLDLogon[OLDLogon.PropertyIDs[idx2]]); break; } } QE_Details.Add(ci.Attributes.PropertyIDs[idx], logonDetails); break; default: QE_Details.Add(ci.Attributes.PropertyIDs[idx], ci.Attributes[ci.Attributes.PropertyIDs[idx]]); break; } } //now replace all existing connections with new one CrystalDecisions.ReportAppServer.DataDefModel.ConnectionInfo newConnInfo = new CrystalDecisions.ReportAppServer.DataDefModel.ConnectionInfo(); newConnInfo.Attributes = QE_Details; newConnInfo.Kind = CrystalDecisions.ReportAppServer.DataDefModel.CrConnectionInfoKindEnum.crConnectionInfoKindCRQE; newConnInfo.UserName = DBUsername; newConnInfo.Password = DBPassword; foreach (CrystalDecisions.ReportAppServer.DataDefModel.ConnectionInfo oldConnInfo in DBController.GetConnectionInfos(null)) { DBController.ReplaceConnection(oldConnInfo, newConnInfo.Clone(true), null, CrystalDecisions.ReportAppServer.DataDefModel.CrDBOptionsEnum.crDBOptionDoNotVerifyDB); }

我希望这个帮助