簡體   English   中英

Crystal Report在發布時要求數據庫登錄

[英]Crystal Report ask for database login while it is published

我正在WPF中開發一個需要從中獲取報告的應用程序,我完成了它,在我的PC中一切正常,沒有出現數據庫登錄對話框,並且完全可以。 但是,當我要將其發布在客戶端PC上並希望通過Crystal Report Viewer顯示報表時,將出現數據庫登錄對話框! 我做了我想過的所有事情,並在網絡上到處搜索,盡了最大的努力,但無法解決。 這是我的代碼:

ReportDocument CryRpt1 = new ReportDocument();
string Path1 = System.Windows.Forms.Application.StartupPath + "\\CryReportSendItems.rpt";
CryRpt1.Load(Path1, OpenReportMethod.OpenReportByDefault);
AssignConnection(CryRpt1);
CryRpt1.Refresh();
Crv.ViewerCore.ReportSource = CryRpt1;

這是AssignConnection方法:

private void AssignConnection(ReportDocument rpt)
        {
            ConnectionInfo connection = new ConnectionInfo();        
            connection.ServerName = "*ServerName*";
            connection.DatabaseName = "*DBName*";
            connection.UserID = "*User*";
            connection.Password = "*Password*";

            foreach (CrystalDecisions.CrystalReports.Engine.Table table in rpt.Database.Tables)
            {
                AssignTableConnection(table, connection);
            }

            foreach (CrystalDecisions.CrystalReports.Engine.Section section in rpt.ReportDefinition.Sections)
            {

                foreach (CrystalDecisions.CrystalReports.Engine.ReportObject reportObject in section.ReportObjects)
                {
                    if (reportObject.Kind == ReportObjectKind.SubreportObject)
                    {
                        SubreportObject subReport = (SubreportObject)reportObject;
                        ReportDocument subDocument = subReport.OpenSubreport(subReport.SubreportName);

                        foreach (CrystalDecisions.CrystalReports.Engine.Table table in subDocument.Database.Tables)
                        {
                            AssignTableConnection(table, connection);
                        }

                        subDocument.SetDatabaseLogon(connection.UserID, connection.Password, connection.ServerName, connection.DatabaseName);
                    }
                }
            }
            rpt.SetDatabaseLogon(connection.UserID, connection.Password, connection.ServerName, connection.DatabaseName);
        }

        private void AssignTableConnection(CrystalDecisions.CrystalReports.Engine.Table table, ConnectionInfo connection)
        {
            // Cache the logon info block
            TableLogOnInfo logOnInfo = table.LogOnInfo;

            connection.Type = logOnInfo.ConnectionInfo.Type;

            // Set the connection
            logOnInfo.ConnectionInfo = connection;

            // Apply the connection to the table!

            table.LogOnInfo.ConnectionInfo.DatabaseName = connection.DatabaseName;
            table.LogOnInfo.ConnectionInfo.ServerName = connection.ServerName;
            table.LogOnInfo.ConnectionInfo.UserID = connection.UserID;
            table.LogOnInfo.ConnectionInfo.Password = connection.Password;
            table.LogOnInfo.ConnectionInfo.Type = connection.Type;
            table.ApplyLogOnInfo(logOnInfo);
        }

*注意:我的Windows版本是10(x64),客戶端Windows版本是7(x64)。

可以幫我嗎?

我找到了答案,也許有人會這樣:

使用Entity Framework在代碼中再次填寫報告。

        using (var db = new DatabaseContext())
        {
            CryRpt1.SetDataSource(db.VwSendItems);
        }

在此之前:

        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder("Data Source=*Server*;Initial Catalog=*DBCenter*;Persist Security Info=True;User ID=*User*;password=*Password*");  //from config.xml
        crConnectionInfo.ServerName = builder.DataSource;
        crConnectionInfo.DatabaseName = builder.InitialCatalog;
        crConnectionInfo.IntegratedSecurity = true;

        CrTables = CryRpt1.Database.Tables;
        foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
        {
            crtableLogoninfo = CrTable.LogOnInfo;
            crtableLogoninfo.ConnectionInfo = crConnectionInfo;
            CrTable.ApplyLogOnInfo(crtableLogoninfo);
        }

        //AssignConnection(CryRpt1);
        ConnectionInfo connection = new ConnectionInfo
        {
            ServerName = "10.20.1.41",
            DatabaseName = "DBCenterMostafavi",
            UserID = "site",
            Password = "123!@#a"
        };
        CryRpt1.SetDatabaseLogon(connection.UserID, connection.Password, connection.ServerName, connection.DatabaseName);

        foreach (CrystalDecisions.CrystalReports.Engine.Table table in CryRpt1.Database.Tables)
        {
            AssignTableConnection(table, connection);
        }

就是這樣

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM