如何在Crystal报表和ms访问中动态更改登录信息?

时间:2010-11-23 09:09:33

标签: vb.net ms-access crystal-reports

我的水晶报告文件从msaccess数据库访问数据。

现在在加载报告时我需要传递msaccess数据库的登录信息以及来自vb.net的数据库名称。

我尝试使用

    Dim ConnInfo As ConnectionInfo = New ConnectionInfo()

    CRpt.ReportOptions.EnableSaveDataWithReport = False

    ConnInfo.IntegratedSecurity = False
    ConnInfo.ServerName = ""
    ConnInfo.UserID = ""
    ConnInfo.Password = ""
    ConnInfo.DatabaseName = OLEDBLayer.GetDBLocation()
    ConnInfo.Type = ConnectionInfoType.DBFile

    'CCINFo.ServerName = 
    For Each CTable As Table In CRpt.Database.Tables
        CTableLogInfo = CTable.LogOnInfo
        CTableLogInfo.ConnectionInfo = ConnInfo
        'CTable.Location = OLEDBLayer.GetDBLocation
        CTable.ApplyLogOnInfo(CTableLogInfo)
    Next

但不行。我错过了什么?

3 个答案:

答案 0 :(得分:1)

您可以使用以下代码在运行时为报告应用某些连接详细信息 对不起,代码在c#。
请在加载报告rpt文件后,打印/导出/查看之前使用该方法。

    public static void CrystalReportLogOn(ReportDocument reportParameters,
                                          string serverName,
                                          string databaseName,
                                          string userName,
                                          string password)
    {
        TableLogOnInfo logOnInfo;
        ReportDocument subRd;
        Sections sects;
        ReportObjects ros;
        SubreportObject sro;

        if (reportParameters == null)
        {
            throw new ArgumentNullException("reportParameters");
        }

        try
        {
            foreach (CrystalDecisions.CrystalReports.Engine.Table t in reportParameters.Database.Tables)
            {
                logOnInfo = t.LogOnInfo;
                logOnInfo.ReportName = reportParameters.Name;
                logOnInfo.ConnectionInfo.ServerName = serverName;
                logOnInfo.ConnectionInfo.DatabaseName = databaseName;
                logOnInfo.ConnectionInfo.UserID = userName;
                logOnInfo.ConnectionInfo.Password = password;
                logOnInfo.TableName = t.Name;
                t.ApplyLogOnInfo(logOnInfo);
                t.Location = t.Name;
            }
        }
        catch
        {
            throw;
        }

        sects = reportParameters.ReportDefinition.Sections;
        foreach (Section sect in sects)
        {
            ros = sect.ReportObjects;
            foreach (ReportObject ro in ros)
            {
                if (ro.Kind == ReportObjectKind.SubreportObject)
                {
                    sro = (SubreportObject)ro;
                    subRd = sro.OpenSubreport(sro.SubreportName);
                    try
                    {
                        foreach (CrystalDecisions.CrystalReports.Engine.Table t in subRd.Database.Tables)
                        {
                            logOnInfo = t.LogOnInfo;
                            logOnInfo.ReportName = reportParameters.Name;
                            logOnInfo.ConnectionInfo.ServerName = serverName;
                            logOnInfo.ConnectionInfo.DatabaseName = databaseName;
                            logOnInfo.ConnectionInfo.UserID = userName;
                            logOnInfo.ConnectionInfo.Password = password;
                            logOnInfo.TableName = t.Name;
                            t.ApplyLogOnInfo(logOnInfo);
                        }
                    }
                    catch
                    {
                        throw;
                    }
                }
            }
        }
    }

答案 1 :(得分:1)

我搜索了很多,最后在通过使用静态数据创建新报告测试我自己后,我得到了动态解决方案。

我的测试程序:
1.)创建新的Crystal Report文件:
2.)创建.accdb访问2007文件的新数据库连接
3.)创建新表格
4.)在表单上添加了Crystal Report Viewer控件 5.)将报告文档分配给先前创建的报告 6.)在形式的负载上设置断点 7.)阅读所有设置
8.)复制报告文档的设置,文档表登录信息 9.)将读取的设置粘贴到我的项目中 10.)工作得很好...... :)

我的代码:

    //here crpt is a sample report document
    Dim CTableLogInfo As TableLogOnInfo
    Dim ConnInfo As CrystalDecisions.Shared.ConnectionInfo = New ConnectionInfo()
    ConnInfo.Type = ConnectionInfoType.CRQE
    ConnInfo.ServerName = DBLayer.GetAbsoluteDBPath()
    ConnInfo.DatabaseName = ""
    ConnInfo.UserID = "Admin"
    ConnInfo.AllowCustomConnection = False
    ConnInfo.IntegratedSecurity = False

    For Each CTable As Table In CRpt.Database.Tables
        CTable.LogOnInfo.ConnectionInfo = ConnInfo
        CTableLogInfo = CTable.LogOnInfo
        CTableLogInfo.ReportName = CRpt.Name
        CTableLogInfo.TableName = CTable.Name
        CTable.ApplyLogOnInfo(CTableLogInfo)
    Next

    CrystalReportViewer1.ReportSource = CRpt
    CrystalReportViewer1.RefreshReport()

我认为将数据库路径设置为 servername 会解决该问题

答案 2 :(得分:1)

试试这个:

        Dim CTableLogInfo As TableLogOnInfo
        Dim ConnInfo As CrystalDecisions.Shared.ConnectionInfo = New ConnectionInfo()
        Dim  CRpt As New ReportDocument
        String filename = "rptSales.rpt"

        ConnInfo.Type = ConnectionInfoType.CRQE
        ConnInfo.ServerName = AppSettings("server")
        ConnInfo.DatabaseName = AppSettings("dbNm")
        ConnInfo.UserID = AppSettings("username")
        ConnInfo.Password = AppSettings("pas")
        ConnInfo.AllowCustomConnection = False
        ConnInfo.IntegratedSecurity = False

        CRpt.Load(AppSettings("reppath") & filename)

        For Each CTable As Table In CRpt.Database.Tables
            CTable.LogOnInfo.ConnectionInfo = ConnInfo
            CTableLogInfo = CTable.LogOnInfo
            CTableLogInfo.ReportName = CRpt.Name
            CTableLogInfo.TableName = CTable.Name
            CTable.ApplCTableLogInfo)
        Next