如何动态更改crystal report数据库连接

时间:2014-05-05 10:39:16

标签: c# visual-studio-2012 reporting-services crystal-reports database-connection

我是水晶报道的新手。我尝试使用报告向导visual studio 2012在我的win form c#应用程序中实现水晶报告,所以不知道反手发生了什么。一切都在我的计算机上运行良好,但当我尝试在另一台计算机上安装此连接字符串更改并给出错误。

我尝试了很多像Dynamic Connection string Change这样的链接,但我正在使用报告向导进行设置,所以不知道在哪里使用它。

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

我可以选择attach connection String from app config at run time

3 个答案:

答案 0 :(得分:7)

尝试这样的事情:

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);

答案 1 :(得分:3)

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);

答案 2 :(得分:0)

以下是将所有主报表表和所有子报表表更改为新指定的具有集成身份验证的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);
            }
        }