使用远程SQL Server作为数据源在运行时创建Crystal报表 [英] Create crystal report at runtime using a remote sql server as data source

查看:66
本文介绍了使用远程SQL Server作为数据源在运行时创建Crystal报表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一台服务器上拥有数据库和应用程序(都在运行时创建水晶报表的Web,API和服务总线)。所有这些都可以在运行时完美地创建报表。

I had my database and applications (web, api & service bus that all create crystal reports at runtime) on the same server. All worked perfect creating reports at runtime.

将数据库移动到其他服务器,该服务器仅允许来自应用程序服务器的远程连接。

Moved the database to a different server, which only allow remote connections from the app server.

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

For my database connections, all I had to do in my projects was change the server in the connection string from (local) to the ip address of the database server, and it all works fine.

但是,仅将服务器从(本地)更改为Crystal报表的ip地址似乎不起作用(给出数据库登录失败错误)

However it seems that to just change the server from (local) to the ip address for the crystal reports does not work (give a "Database logon failed" error)

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

I'm not sure if this is the problem, but for creating the report locally (before uploading the .rpt to the server) I had to create a connection where the server is set to "local" ( in the data source location). Since I cannot access the new database remotely from my local machine, I cannot change that)

我使用的代码如下:

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


推荐答案

谢谢,我最终以这个有效的方法(使用sql)
本质上,这个想法不仅是为报表设置登录信息,还为所有子报表设置登录信息。如果可以只执行 foreach子报表在报告中..,那会很好,但似乎不能那样做

Thank you, I ended up with this that worked (using sql) Essentially the idea is setting the logon info not just for the report, but also for all subreports. Would have been nice if one could just do a "foreach subreport in report.." ,but seems not to work that way

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

这篇关于使用远程SQL Server作为数据源在运行时创建Crystal报表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆