如何动态更改水晶报表数据库连接



我是水晶报告的新手。我试图使用报表向导visualstudio2012在我的win-formc应用程序中实现crystal报表,所以不知道会发生什么。在我的电脑上一切都很好,但当我尝试在另一台电脑上安装它时,连接字符串发生了变化并出错。

我尝试了很多链接,比如动态连接字符串更改,但由于我正在使用报告向导进行设置,所以不知道在哪里使用它。

我还尝试了报告向导中连接字符串的所有选项,但在运行时没有发现任何更改连接字符串的内容。

有什么选项可以让我attach connection String from app config at run time吗。

试试这样的东西:

strServer= ConfigurationManager.AppSettings["ServerName"].ToString();
strDatabase= ConfigurationManager.AppSettings["DataBaseName"].ToString();
strUserID= ConfigurationManager.AppSettings["UserId"].ToString();
strPwd= ConfigurationManager.AppSettings["Password"].ToString();
report.DataSourceConnections[0].SetConnection(strServer, strDatabase, strUserID, strPwd);
strServer= ConfigurationManager.AppSettings["ServerName"].ToString();
strDatabase= ConfigurationManager.AppSettings["DataBaseName"].ToString();
strUserID= ConfigurationManager.AppSettings["UserId"].ToString();
strPwd= ConfigurationManager.AppSettings["Password"].ToString();
//may be you need to set the integrated security to false, first.
report.DataSourceConnections[o].IntegratedSecurity = False;
report.DataSourceConnections[0].SetConnection(strServer, strDatabase, strUserID, strPwd);

以下是将所有主报表表以及所有子报表表更改为新指定的具有集成身份验证的TargetServer和TargetDatabase的示例:

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
// using CrystalDecisions.ReportAppServer.CommLayer; // not used directly, but this is needed in Project References.
//
// be sure to set "copy local" = true in the Project:
// see https://stackoverflow.com/questions/38025601/could-not-load-file-or-assembly-crystaldecisions-reportappserver-commlayer-ver

        static ReportDocument crReportDocument;
        static ConnectionInfo crConnectionInfo = new ConnectionInfo();
        static public string TargetServer { get; set; }
        static public string TargetDatabase { get; set; }
        static void crAssignConnectionInfo()
        {
            crConnectionInfo.UserID = "";
            crConnectionInfo.Password = "";
            crConnectionInfo.DatabaseName = TargetDatabase;
            crConnectionInfo.ServerName = TargetServer;
            crConnectionInfo.IntegratedSecurity = true; // in case the report was saved with SQL authentication, switch to Integrated
        }
        static void SetSubreportLoginInfo(CrystalDecisions.CrystalReports.Engine.Sections objSections)
        {
            foreach (Section section in objSections)
            {
                foreach (ReportObject reportObject in section.ReportObjects)
                {
                    SubreportObject crSubreportObject;
                    switch (reportObject.Kind)
                    {
                        case ReportObjectKind.SubreportObject:
                            crSubreportObject = (SubreportObject)reportObject;
                            ReportDocument subRepDoc = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);
                            if (subRepDoc.ReportDefinition.Sections.Count > 0) {
                                SetSubreportLoginInfo(subRepDoc.ReportDefinition.Sections);
                            }
                            Tables crTables = subRepDoc.Database.Tables;
                            foreach (Table table in crTables)
                            {
                                TableLogOnInfo tableLogOnInfo = new TableLogOnInfo();
                                tableLogOnInfo.ConnectionInfo.UserID = crConnectionInfo.UserID;
                                tableLogOnInfo.ConnectionInfo.Password = crConnectionInfo.Password;
                                tableLogOnInfo.ConnectionInfo.DatabaseName = crConnectionInfo.DatabaseName;
                                tableLogOnInfo.ConnectionInfo.ServerName = crConnectionInfo.ServerName;
                                tableLogOnInfo.ConnectionInfo.IntegratedSecurity = crConnectionInfo.IntegratedSecurity;
                                table.ApplyLogOnInfo(tableLogOnInfo);
                            }
                            break;
                        case ReportObjectKind.FieldObject:
                        case ReportObjectKind.TextObject:
                        case ReportObjectKind.LineObject:
                        case ReportObjectKind.BoxObject:
                        case ReportObjectKind.PictureObject:
                        case ReportObjectKind.ChartObject:
                        case ReportObjectKind.CrossTabObject:
                        case ReportObjectKind.BlobFieldObject:
                        case ReportObjectKind.MapObject:
                        case ReportObjectKind.OlapGridObject:
                        case ReportObjectKind.FieldHeadingObject:
                        case ReportObjectKind.FlashObject:
                        default:
                            // none of the other objects need to have login assigned
                            break;
                    }
                }
            }
        }
        static void SetCrystalDocumentLogon()
        {
            crAssignConnectionInfo();
            TableLogOnInfo crTableLogonInfo = new TableLogOnInfo();
            foreach (Table crTable in crReportDocument.Database.Tables)
            {
                try
                {
                    crConnectionInfo.Type = crTable.LogOnInfo.ConnectionInfo.Type;
                    crTableLogonInfo.ConnectionInfo = crConnectionInfo;
                    crTableLogonInfo.ReportName = crTable.LogOnInfo.ReportName;
                    crTableLogonInfo.TableName = crTable.LogOnInfo.TableName;
                    crTable.ApplyLogOnInfo(crTableLogonInfo);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error during SetCrystalDocumentLogon " + ex.Message);
                    throw;
                }
                SetSubreportLoginInfo(crReportDocument.ReportDefinition.Sections);
            }
        }

最新更新