2014-03-05 44 views
0

這是我的代碼,當我打開我的水晶報告時,它總是給我一個需要填寫用戶名和密碼的表單。我想禁用它。另一個問題是,當我在我的計算機中安裝我的項目時,我的項目正常運行,但仍然需要填寫用戶和密碼,並且如果我安裝到網絡或其他計算機上,當我在本地主機中填寫相同的用戶和密碼時,一個錯誤。如何將我的水晶報告連接到網絡

私人小組AssignConnection(BYVAL RPT作爲的ReportDocument) 昏暗的連接作爲新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 
+0

你可能會更具體一些c你想做什麼? 「網絡」在這種情況下沒有意義。 – user2721815

+0

對不起,我編輯了我的評論。你能幫我嗎? – user3336059

回答

0

嘗試一下


公用Sub LogOnToDatabase(爲ByRef pCrystalReport作爲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 
+0

請添加更多關於您的答案的描述和/或信息(可能指出哪些部分需要編輯)以及如何解決問題,以便其他人可以輕鬆理解而不要求澄清 – koceeng