How to dynamically change crystal report database connection

后端 未结 3 564
自闭症患者
自闭症患者 2020-12-17 04:15

I am new with crystal reports. I tried to to implement the crystal report in my win form c# application using report wizard visual studio 2012, so don\'t know what happen\'s

相关标签:
3条回答
  • 2020-12-17 04:44

    Here's an example of changing all the main report tables as well as all subreports tables, to a newly specified TargetServer and TargetDatabase with Integrated Authentication:

    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);
                }
            }
    
    0 讨论(0)
  • 2020-12-17 04:55

    Try something like this:

    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);
    
    0 讨论(0)
  • 2020-12-17 05:06
    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);
    
    0 讨论(0)
提交回复
热议问题