如何从xml动态获取水晶报表中的数据源

时间:2014-04-30 10:05:03

标签: xml vb.net vb6 crystal-reports

我有一个报告,其数据源是sql server。我希望报告能够动态选择不同的数据库,这样如果我使用vb app调用它,它可以选择一个数据库。我认为数据库和密码是从xml获取的文献。 请帮助其他方法。谢谢。

 Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    CrystalReportViewer1.ReportSource = ("C:\Users\Martin\Desktop\working\EMPLOYEES.rpt")
    CrystalReportViewer1.Show()
End Sub

我的vb代码调用上面的报告。

1 个答案:

答案 0 :(得分:1)

solution`Imports CrystalDecisions.CrystalReports.Engine 导入CrystalDecisions.Shared 导入System.Xml

Public Class Form2

Private Sub CrystalReportViewer1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CrystalReportViewer1.Load
    Me.Left = 0
    Me.Top = 0
    Me.Height = 10000
    Me.Width = 18408

End Sub

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim crReportDocument As New ReportDocument()
    Dim crtableLogoninfos As New TableLogOnInfos()
    Dim crtableLogoninfo As New TableLogOnInfo()
    Dim crConnectionInfo As New ConnectionInfo()
    Dim CrTables As Tables
    Dim CrTable As Table
    'Dim reader As XmlTextReader = New XmlTextReader("C:\Users\Martin\Desktop\About2Go_H.A.M\working\db.xml")
    Dim document As XDocument = XDocument.Load("c:\Users\Martin\Desktop\About2Go_H.A.M\working\db.xml")
    Dim title = From t In document.Descendants("ServerName") Select t.Value
    Dim ServerName1 = title.First()
    Dim title2 = From t In document.Descendants("DatabaseName") Select t.Value
    Dim DatabaseName1 = title2.First()
    Dim title3 = From t In document.Descendants("UserID") Select t.Value
    Dim UserID1 = title3.First()
    Dim title4 = From t In document.Descendants("Password") Select t.Value
    Dim password1 = title4.First()
    crReportDocument.Load("C:\Users\Martin\Desktop\About2Go_H.A.M\working\EMPLOYEES2.rpt")
    CrystalReportViewer1.ReportSource = crReportDocument
    CrTables = crReportDocument.Database.Tables

    Dim crLoc As String
    crLoc = UserID1 & ".dbo"

    For Each CrTable In CrTables

        crtableLogoninfo = CrTable.LogOnInfo
        'Read MachineName\InstanceName,Database details from User interface
        'and load them into crConnectionInfo object
        crConnectionInfo.ServerName = ServerName1
        crConnectionInfo.DatabaseName = DatabaseName1
        crConnectionInfo.UserID = UserID1
        crConnectionInfo.Password = password1
        crConnectionInfo.IntegratedSecurity = False
        crtableLogoninfo.ConnectionInfo = crConnectionInfo
        CrTable.ApplyLogOnInfo(crtableLogoninfo)
        CrTable.Location.Substring(CrTable.Location.LastIndexOf(".") + 1)

    Next

    crReportDocument.ReportOptions.EnableSaveDataWithReport = False
    'Refresh the ReportViewer Object
    CrystalReportViewer1.RefreshReport()
    'Bind the ReportDocument to ReportViewer Object
    CrystalReportViewer1.ReportSource = crReportDocument
End Sub

结束课程