Crystal报表创建与OLE DB数据库的连接

时间:2009-11-09 08:22:02

标签: crystal-reports

我正在使用VS 2005并使用visual basic进行编码。

如何使用vb代码为我的水晶报告设置连接对象。

我写了一些代码

Dim strcon As String = ConfigurationManager.AppSettings("PhdConnectionString")
Dim getconn As SqlConnection = New SqlConnection(strcon)

Dim rpt As ReportDocument = New ReportDocument

        rpt.Load(Server.MapPath("aspirantCrystalReport.rpt"))

//我想在这里设置连接属性。我该怎么做?

        CrystalReportViewer1.ReportSource = rpt
        CrystalReportViewer1.DataBind()

1 个答案:

答案 0 :(得分:1)

我没有使用Crystal Reports .NET API完成此操作,但我确实有一段用VB6编写的代码调用COM API。 API类和成员名称不能 不同。

Private Sub SetDataConnections(ByVal oReport As CRAXDRT.Report, ByVal oConnection As ADODB.Connection)

    ' Do all tables in this report.
    Dim oTable As CRAXDRT.DatabaseTable
    For Each oTable In oReport.Database.Tables
        SetDataConnection oTable, oConnection
    Next

    ' Find all subreports and do them too.
    Dim oSection As CRAXDRT.Section
    For Each oSection In oReport.Sections
        Dim oObject As Object
        For Each oObject In oSection.ReportObjects
            If TypeOf oObject Is CRAXDRT.SubreportObject Then
                Dim oSubreportObject As CRAXDRT.SubreportObject
                Set oSubreportObject = oObject
                SetDataConnections oSubreportObject.OpenSubreport()
                Set oSubreportObject = Nothing
            End If
        Next
    Next

End Sub

Private Sub SetDataConnection(ByVal oTable As CRAXDRT.DatabaseTable, ByVal oConnection As ADODB.Connection)

    ' Extract the relevant data from the ADO connection.
    Dim sServer As String
    Dim sDatabase As String
    Dim bTrusted As String
    Dim sUserName As String
    Dim sPassword As String
    Dim nTimeout As Long
    With oConnection.Properties
        sServer = .Item("Data Source").Value
        sDatabase = .Item("Initial Catalog").Value
        Select Case UCase(.Item("Integrated Security").Value)
        Case "SSPI", "YES"
            bTrusted = True
        Case Else ' "NO", ""
            bTrusted = False
        End Select
        sUserName = .Item("User ID").Value
        sPassword = .Item("Password").Value
    End With
    nTimeout = oConnection.CommandTimeout

    ' Delete and re-create all connection information. This is the only way of getting it
    ' to work if the report contains subreports. Changing database drivers on the fly is
    ' not allowed, so we must re-create the connection using settings appropriate for the
    ' particular driver involved.
    Select Case oTable.DllName
    Case "crdb_ado.dll"
        With oTable.ConnectionProperties
            .DeleteAll
            .Add "Database Type", "OLE DB (ADO)"
            .Add "Provider", "SQLOLEDB"
            .Add "Data Source", sServer
            .Add "Initial Catalog", sDatabase
            .Add "Integrated Security", bTrusted
            .Add "User ID", sUserName
            .Add "Password", sPassword
            .Add "OLE DB Services", -1
            .Add "General Timeout", nTimeout
        End With
    Case Else
        ' TODO: Handle other drivers appropriately.
    End Select

End Sub