首页 文章

如何将我的水晶报告连接到网络中

提问于
浏览
0

这是我的代码,当我打开我的水晶报告时,它总是给我一个需要填写用户名和密码的表格 . 我想禁用它 . 另一个问题是当我在我的计算机上安装我的项目时,我的项目正确运行但仍然需要填写用户和密码,如果我安装到网络或其他计算机,当我在本地主机中填写用户和密码时,它给我一个错误 .

Private Sub AssignConnection(ByVal rpt As ReportDocument)Dim connection As New ConnectionInfo()

connection.DatabaseName = "pcba_info" 
    connection.ServerName = "192.168.0.201" 
    connection.UserID = "partschecker" 
    connection.Password = "sgic" 

    For Each table As CrystalDecisions.CrystalReports.Engine.Table In rpt.Database.Tables
        AssignTableConnection(table, connection)
    Next

    ' Now loop through all the sections and its objects to do the same for the subreports
    '
    For Each section As CrystalDecisions.CrystalReports.Engine.Section In rpt.ReportDefinition.Sections
        ' In each section we need to loop through all the reporting objects
        For Each reportObject As CrystalDecisions.CrystalReports.Engine.ReportObject In section.ReportObjects
            If reportObject.Kind = ReportObjectKind.SubreportObject Then
                Dim subReport As SubreportObject = DirectCast(reportObject, SubreportObject)
                Dim subDocument As ReportDocument = subReport.OpenSubreport(subReport.SubreportName)

                For Each table As CrystalDecisions.CrystalReports.Engine.Table In subDocument.Database.Tables
                    AssignTableConnection(table, connection)
                Next

                subDocument.SetDatabaseLogon(connection.UserID, connection.Password, connection.ServerName, connection.DatabaseName)
            End If
        Next
    Next
    rpt.SetDatabaseLogon(connection.UserID, connection.Password, connection.ServerName, connection.DatabaseName)
End Sub


Private Sub AssignTableConnection(ByVal table As CrystalDecisions.CrystalReports.Engine.Table, ByVal connection As ConnectionInfo)
    ' Cache the logon info block
    Dim logOnInfo As TableLogOnInfo = 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)
End Sub

1 回答

  • 0

    试试看


    Public Sub LogOnToDatabase(ByRef pCrystalReport As CrystalDecisions.CrystalReports.Engine.ReportDocument)

    Dim objTableLogonInfo As CrystalDecisions.Shared.TableLogOnInfo
    
        '\ Report objects
        Dim objDatabaseTable As CrystalDecisions.CrystalReports.Engine.Table
        Dim objCrSection As CrystalDecisions.CrystalReports.Engine.Section
        Dim objCrReportObject As CrystalDecisions.CrystalReports.Engine.ReportObject
        Dim objCrSubreportObject As CrystalDecisions.CrystalReports.Engine.SubreportObject
        Dim objCrSubreport As CrystalDecisions.CrystalReports.Engine.ReportDocument
    
        '\ Call you code to get the connection info from the INI or txt file
        objTableLogonInfo = New CrystalDecisions.Shared.TableLogOnInfo
        objTableLogonInfo.ConnectionInfo.UserID = "UserID " ' UserIDFromFile
        objTableLogonInfo.ConnectionInfo.Password = "Password " ' PasswordFromFile
        objTableLogonInfo.ConnectionInfo.ServerName = "ServerName " ' ServerNameFromFile
        objTableLogonInfo.ConnectionInfo.DatabaseName = "DatabaseName " ' DatabaseNameFromFile
    
    
        '\ Loop through the tables in the database and set the connection properties of each
        For Each objDatabaseTable In pCrystalReport.Database.Tables
            objDatabaseTable.ApplyLogOnInfo(objTableLogonInfo)
        Next objDatabaseTable
    
        '\ Now do the same for the subreports(if any)
        '\ Loop through the sections in the report
        For Each objCrSection In pCrystalReport.ReportDefinition.Sections
    
            '\ Loop through the collection
            For Each objCrReportObject In objCrSection.ReportObjects
    
                '\ If the report object is a subreport
                If objCrReportObject.Kind = CrystalDecisions.Shared.ReportObjectKind.SubreportObject Then
    
                    '\ Get a reference to it
                    objCrSubreportObject = objCrReportObject
    
                    '\ Open it
                    objCrSubreport = objCrSubreportObject.OpenSubreport(objCrSubreportObject.SubreportName)
    
                    '\ Set logon info for each table in the subreport
                    For Each objDatabaseTable In objCrSubreport.Database.Tables
                        objDatabaseTable.ApplyLogOnInfo(objTableLogonInfo)
                    Next objDatabaseTable
    
                End If
            Next objCrReportObject
        Next objCrSection
    End Sub
    

相关问题