VB.Net水晶报告连接字符串

时间:2013-12-08 07:45:49

标签: vb.net string crystal-reports connection report

我正在使用vb.net 2010来开发我的软件。在我的项目中也有crystal reports,而且我的电脑里的东西也很完美。

我的问题是我使用向导和my PC is not the server在我的电脑中设计水晶报告,然后将其上传到服务器,以便用户可以访问。但是当试图打开报告时,弹出一个connection problem到数据库。我知道这是由于我在PC上设计报告时的连接属性。

我该如何解决这个问题。

5 个答案:

答案 0 :(得分:4)

显示提供用户ID和密码的弹出窗口。我想以编程方式提供服务器连接。

enter image description here

我给出了以下代码,它仍显示弹出窗口

Private Sub CrystalReportViewer1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles CrystalReportViewer1.Load
        Dim cryRpt 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
        cryRpt.Load("E:\ColorLab1\colorlab\colorlab\rpt_bill.rpt")

        With crConnectionInfo
            .ServerName = "MAHESH\SQLEXPRESS"
            .DatabaseName = "cc"
            .UserID = "erp"
            .Password = "123"
        End With

        CrTables = cryRpt.Database.Tables
        For Each CrTable In CrTables
            crtableLogoninfo = CrTable.LogOnInfo
            crtableLogoninfo.ConnectionInfo = crConnectionInfo
            CrTable.ApplyLogOnInfo(crtableLogoninfo)
        Next
        CrystalReportViewer1.RefreshReport()
    End Sub

我该如何解决这个问题?

答案 1 :(得分:2)

所以要把它写成一个正确的答案:

通过在PC上创建与PC上相同的DSN,可以解决第一个问题。 您的第二个问题可以使用类似下面的代码来解决:

connection.DatabaseName = [DatabaseName]
connection.UserID = [UserID]
connection.ServerName = [ServerName]
connection.Password = [Password]

myCrystalReport.SetDatabaseLogon("myUsername", "myPassword","servername","dbname");

答案 2 :(得分:2)

在模块中添加此代码(用于公共访问)

 Public Sub SetReportDb(ByVal ConnectionString As String, ByRef CrystalReportViewer As CrystalDecisions.Windows.Forms.CrystalReportViewer, ByRef reportDocument As ReportClass)
        'Get SQL Server Details
        Dim builder As New System.Data.Common.DbConnectionStringBuilder()

        builder.ConnectionString = ConnectionString


        Dim zServer As String = TryCast(builder("Data Source"), String)
        Dim zDatabase As String = TryCast(builder("Initial Catalog"), String)
        Dim zUsername As String = TryCast(builder("User ID"), String)
        Dim zPassword As String = TryCast(builder("Password"), String)

        Dim ciReportConnection As New ConnectionInfo

        ciReportConnection.ServerName = zServer
        ciReportConnection.DatabaseName = zDatabase
        ciReportConnection.UserID = zUsername
        ciReportConnection.Password = zPassword

        'Assign data source details to tables

        For Each table As Table In reportDocument.Database.Tables
            table.LogOnInfo.ConnectionInfo = ciReportConnection
            table.ApplyLogOnInfo(table.LogOnInfo)
        Next

        For Each subrep As ReportDocument In reportDocument.Subreports
            For Each table As Table In subrep.Database.Tables
                table.LogOnInfo.ConnectionInfo = ciReportConnection
                table.ApplyLogOnInfo(table.LogOnInfo)
            Next
        Next

        'Assign data source details to the report viewer
        If CrystalReportViewer.LogOnInfo IsNot Nothing Then
            Dim tlInfo As TableLogOnInfos = CrystalReportViewer.LogOnInfo
            For Each tbloginfo As TableLogOnInfo In tlInfo
                tbloginfo.ConnectionInfo = ciReportConnection
            Next
        End If
        reportDocument.VerifyDatabase()
        reportDocument.Refresh()
        CrystalReportViewer.ReportSource = reportDocument
        CrystalReportViewer.Refresh()
    End Sub

在每个水晶报表查看器中,给出以下代码,它将覆盖与connectionstring的旧连接

 Private Sub CrystalReportViewer1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CrystalReportViewer1.Load
        SetReportDb(My.Settings.colorlabConnectionString, CrystalReportViewer1, rpt_inwardreport1)
    End Sub

答案 3 :(得分:1)

大家好我得到了答案

 Private Sub rpt_billform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Me.CrystalReportViewer1.LogOnInfo.Item(0).ConnectionInfo.ServerName = "MAHESH\SQLEXPRESS"
    Me.CrystalReportViewer1.LogOnInfo.Item(0).ConnectionInfo.DatabaseName = "cc"
    Me.CrystalReportViewer1.LogOnInfo.Item(0).ConnectionInfo.UserID = "erp"
    Me.CrystalReportViewer1.LogOnInfo.Item(0).ConnectionInfo.Password = "123"
End Sub

答案 4 :(得分:0)

我知道这篇文章已有几年历史了,但在努力解决这些问题之后,我想我会为MS SQL 2016服务器添加一个Mahesh ML例程的变体。

有三点需要注意:

    使用
  • SqlClient.SqlConnectionStringBuilder代替DbConnectionStringBuilder
  • zSecurity(已添加)用于Window的身份验证而非数据库用户
  • 使用Windows身份验证时需要
  • reportDocument.DataSourceConnections(0).IntegratedSecurity = True

    Public Sub SetReportSQL(ByVal ConnectionString As String,
                       ByRef CrystalReportViewer As CrystalDecisions.Windows.Forms.CrystalReportViewer,
                       ByRef reportDocument As ReportClass)
        'Get SQL Server Details
        Dim builder As New SqlClient.SqlConnectionStringBuilder
    
        builder.ConnectionString = ConnectionString
    
        Dim zServer As String = TryCast(builder("Data Source"), String)
        Dim zDatabase As String = TryCast(builder("Initial Catalog"), String)
        Dim zSecurity As Boolean = Boolean.TryParse(builder("Integrated Security"), zSecurity)
        Dim zUsername As String = TryCast(builder("User ID"), String)
        Dim zPassword As String = TryCast(builder("Password"), String)
    
        Dim ciReportConnection As New ConnectionInfo
    
        ciReportConnection.ServerName = zServer
        ciReportConnection.DatabaseName = zDatabase
        ciReportConnection.IntegratedSecurity = zSecurity
        If zSecurity = False Then
            ciReportConnection.UserID = zUsername
            ciReportConnection.Password = zPassword
        Else
            reportDocument.DataSourceConnections(0).IntegratedSecurity = True
        End If
    
        'Assign data source details to tables
        For Each table As Table In reportDocument.Database.Tables
            table.LogOnInfo.ConnectionInfo = ciReportConnection
            table.ApplyLogOnInfo(table.LogOnInfo)
        Next
    
        For Each subrep As ReportDocument In reportDocument.Subreports
            For Each table As Table In subrep.Database.Tables
                table.LogOnInfo.ConnectionInfo = ciReportConnection
                table.ApplyLogOnInfo(table.LogOnInfo)
            Next
        Next
    
        'Assign data source details to the report viewer
        If CrystalReportViewer.LogOnInfo IsNot Nothing Then
            Dim tlInfo As TableLogOnInfos = CrystalReportViewer.LogOnInfo
            For Each tbloginfo As TableLogOnInfo In tlInfo
                tbloginfo.ConnectionInfo = ciReportConnection
            Next
        End If
        reportDocument.VerifyDatabase()
        reportDocument.Refresh()
        CrystalReportViewer.ReportSource = reportDocument
        CrystalReportViewer.Refresh()
    End Sub