Crystal Report:报表无法按指定的方式连接到数据库

时间:2011-03-09 09:42:52

标签: asp.net crystal-reports

我们已在目标计算机上部署了ASP.NET应用程序。该应用程序包含一些水晶报告(rpt文件),并在调用时传递数据库连接详细信息。它在测试服务器上工作正常。但是在目标服务器上,调用时的晶体报告会询问数据库连接详细信息。它要求四个参数:服务器,数据库,用户ID,密码。其中,前两个文本框被禁用。服务器编辑框显示我通过的服务器的IP。但数据库参数显示为空白。我期待我在连接字符串中设置的初始目录。用户ID显示我传递的用户ID。

配置文件的连接字符串如下:

<add name = "reportdb" connectionString = "Data Source=172.16.7.85;Initial Catalog=reportdb;Persist Security Info=True;User ID=sa;Password=sa;" providerName = "System.Data.SqlClient"/>

设置值时,我们从配置中获取数据并通过以下方法设置为报告:

public int GenerateReport(CrystalReportViewer crystalReportViewer, string userid, string password, string server,string databaseName)
{
    reportDoc = new ReportDocument();           
    subreportDocument = new ReportDocument();
    Sections sections;
    SubreportObject subreportObject;
    ReportObjects reportObjects;
    ConnectionInfo connectionInfo = new ConnectionInfo();
    TableLogOnInfo tableLogOnInfo = new TableLogOnInfo();
    Database database;
    Tables tables;
    CrystalDecisions.CrystalReports.Engine.Table crTable;
    connectionInfo.DatabaseName = databaseName;
    connectionInfo.UserID = userid;
    connectionInfo.Password = password;
    connectionInfo.ServerName = server;


    reportDoc.Load(reportName);
    database = reportDoc.Database;
    tables = database.Tables;
    tableLogOnInfo.ConnectionInfo = connectionInfo;
    for (int i = 0; i < tables.Count; i++)
    {
        crTable = tables[i];
        tableLogOnInfo = crTable.LogOnInfo;
        tableLogOnInfo.ConnectionInfo = connectionInfo;
        crTable.ApplyLogOnInfo(tableLogOnInfo);
    }

    sections = reportDoc.ReportDefinition.Sections;
    foreach (Section crSection in sections)
    {
        reportObjects = crSection.ReportObjects;
        foreach (ReportObject crReportObject in reportObjects)
        {
            if (crReportObject.Kind == ReportObjectKind.SubreportObject)
            {
                subreportObject = (SubreportObject)crReportObject;
                subreportDocument = subreportObject.OpenSubreport(subreportObject.SubreportName);
                database = subreportDocument.Database;
                tables = database.Tables;
                foreach (CrystalDecisions.CrystalReports.Engine.Table aTable in tables)
                {
                    tableLogOnInfo = aTable.LogOnInfo;
                    tableLogOnInfo.ConnectionInfo = connectionInfo;
                    aTable.ApplyLogOnInfo(tableLogOnInfo);
                }
            }                      
        }
    }

    foreach (DataRow dataRow in reportDetailDataTable.Rows)
    {
        //because it'll be null for optional parameter
        ////if (!string.IsNullOrEmpty(dataRow["Parameter value"].ToString()))
        ////{
            ParameterFieldDefinition parameterFieldDefinition = null;
            ParameterValues parameterValues = null;
            ParameterDiscreteValue parmeterDiscreteValue = null;
            parameterValues = new ParameterValues();
            parameterFieldDefinition = reportDoc.DataDefinition.ParameterFields[dataRow[0].ToString()];
            parmeterDiscreteValue = new ParameterDiscreteValue();
            parmeterDiscreteValue.Value = dataRow[2].ToString();
            parameterValues.Add(parmeterDiscreteValue);
            parameterFieldDefinition.ApplyCurrentValues(parameterValues);

    }
    if (reportDoc.Database.Tables.Count > 0)//i.e only if report has table
    {
        reportDoc.Database.Tables[0].ApplyLogOnInfo(tableLogOnInfo);
    }
    crystalReportViewer.ReportSource = reportDoc;
    return -1;
}

所以,我觉得Crystal report或SQL native客户端存在一些问题。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

如果从web.config本身获取连接属性,该怎么办?根据需要拆分或在

中添加键值
<appsettings add key="Datasource" value="19.168.10.1"> </appsettings>

等等。