将连接信息传递给Crystal Reports [英] Pass connection information to Crystal Reports

查看:43
本文介绍了将连接信息传递给Crystal Reports的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我创建了一个水晶报表,从SQL Server中的一个表中选择4-5个字段。我将水晶报告导入了项目。我将crystalreport viewer和report文档添加到表单中,并创建了下面的代码,将连接信息传递给crystal报表。每次在水晶报表查看器中打开报表或将报表导出为PDF时,即使我在下面的代码中更改了数据库名称,它也始终显示创建水晶报表的数据。是否有人能够帮助我处理正在发生的事情以及如何进行水晶显示并导出代码中定义的连接信息的数据?



私人 Sub Form1_Load( ByVal sender As System.Object, ByVal e < font color ="#0000ff"size = 2> As System.EventArgs) 句柄 MyBase .Load


Dim myTable As CrystalDecisions。 CrystalReports.Engine.Table


Dim ConIn fo As CrystalDecisions.Shared.TableLogOnInfo


对于 每个 myTable In TestReport1.Database.Tables


ConInfo = myTable.LogOnInfo


ConInfo.ConnectionInfo.ServerName =" servername"


ConInfo.ConnectionInfo.UserID =" sa"


ConInfo.ConnectionInfo.Password = ""


ConInfo.ConnectionInfo .DatabaseName =" databasename"


myTable.ApplyLogOnInfo(ConInfo)


下一步 myTable


CrystalReportViewer1.ReportSource = TestReport1


End Sub


私人 Sub Button1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) 句柄 Button1.Click


Dim crDiskFileDestinationOptions As New CrystalDecisions.Shared.DiskFileDestinationOptions()


Dim szFileName As String =&q uot; c:\ windows \ temp \ myreport4.pdf"


crDiskFileDestinationOptions.DiskFileName = szFileName


使用 TestReport1


.ExportOptions.ExportDestinationType = _


CrystalDecisions .Shared.ExportDestinationType.DiskFile


.ExportOptions.ExportFormatType = _


CrystalDecisions.Shared.ExportFormatType.PortableDocFormat


.ExportOptions.DestinationOptions = crDiskFileDestinationOptions


.Export()


结束 使用


结束 Sub

解决方案

我不熟悉VB语法因为我在C#中工作,但是这里有一些来自另一篇文章的代码作者说它有效秒。它比你的复杂一点,因为作者使用了2个不同的数据源,但是你应该能够过滤掉它。

Dim myReport As < font color ="#0000ff">新 ReportDocument


Dim myData As 数据集


昏暗 conn1 作为 MySql.Data.MySqlClient.MySqlConnection


Dim cmd As MySql.Data.MySqlClient.MySqlCommand


Dim myAdapter As MySql.Data.MySqlClient .MySqlDataAdapter


conn1.ConnectionString = " SERVER = mainserver; DATABASE = misdatabase; USER = misuser; PASSWORD = kslmis; PORT = 3308"


尝试


conn1.Open()


cmd.CommandText = " SELECT * FROM asset;" _


& " SELECT * FROM location"


cmd.Connection = conn1


myAdapter.SelectCommand = cmd


myAdapter.Fill(myData)


myReport.Load(Application.StartupPath&(" ; \\Reports \\" & rpttest& " .rpt" ))


myReport.Database.Tables(0).SetDataSource(myData.Tables(0))


myReport.Database.Tables(1).SetDataSource(myData.Tables(1))


myReport.Refresh()


'myReport.RecordSelectionFormula =" {asset.year} ='2007 '<


CRTViewer.ReportSource = myReport


Catch ex < font color ="#0000ff"> As 异常


MessageBox.Show(ex.Message,"无法创建报告" ,MessageBoxButtons.OK,MessageBoxIcon.Error)


结束 尝试

我希望有所帮助。

结果

 

I created a crystal report that selects 4-5 fields from one table in SQL server. I imported the crystal report into the project. I added the crystalreport viewer and report document to the form and created the code below to pass the connection information to the crystal report. Each time that the report opens in the crystal report viewer or when the report is exported to PDF it always displays the data that the crystal report was created with even though I change the database name in the code below. Would anyone be able to assist me with what is happening and how to have crystal display and export the data for the connection information defined in the code?

 

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

Dim myTable As CrystalDecisions.CrystalReports.Engine.Table

Dim ConInfo As CrystalDecisions.Shared.TableLogOnInfo

For Each myTable In TestReport1.Database.Tables

ConInfo = myTable.LogOnInfo

ConInfo.ConnectionInfo.ServerName = "servername"

ConInfo.ConnectionInfo.UserID = "sa"

ConInfo.ConnectionInfo.Password = ""

ConInfo.ConnectionInfo.DatabaseName = "databasename"

myTable.ApplyLogOnInfo(ConInfo)

Next myTable

CrystalReportViewer1.ReportSource = TestReport1

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim crDiskFileDestinationOptions As New CrystalDecisions.Shared.DiskFileDestinationOptions()

Dim szFileName As String = "c:\windows\temp\myreport4.pdf"

crDiskFileDestinationOptions.DiskFileName = szFileName

With TestReport1

.ExportOptions.ExportDestinationType = _

CrystalDecisions.Shared.ExportDestinationType.DiskFile

.ExportOptions.ExportFormatType = _

CrystalDecisions.Shared.ExportFormatType.PortableDocFormat

.ExportOptions.DestinationOptions = crDiskFileDestinationOptions

.Export()

End With

End Sub

解决方案

I'm not familiar with VB syntax because I'm working in C#, but here is some code from another post and the author says that it works.  It is a little more complicated than yours because the author is using 2 different data sources, but you should be able to filter that out. 

Dim myReport As New ReportDocument

Dim myData As New DataSet

Dim conn1 As New MySql.Data.MySqlClient.MySqlConnection

Dim cmd As New MySql.Data.MySqlClient.MySqlCommand

Dim myAdapter As New MySql.Data.MySqlClient.MySqlDataAdapter

conn1.ConnectionString = "SERVER=mainserver;DATABASE=misdatabase;USER=misuser;PASSWORD=kslmis;PORT=3308"

Try

conn1.Open()

cmd.CommandText = "SELECT * FROM asset;" _

& "SELECT * FROM location"

cmd.Connection = conn1

myAdapter.SelectCommand = cmd

myAdapter.Fill(myData)

myReport.Load(Application.StartupPath & ("\\Reports\\" & rpttest & ".rpt"))

myReport.Database.Tables(0).SetDataSource(myData.Tables(0))

myReport.Database.Tables(1).SetDataSource(myData.Tables(1))

myReport.Refresh()

'myReport.RecordSelectionFormula = "{asset.year} = '2007'"

CRTViewer.ReportSource = myReport

Catch ex As Exception

MessageBox.Show(ex.Message, "Report could not be created", MessageBoxButtons.OK, MessageBoxIcon.Error)

End Try

I hope that helps.



这篇关于将连接信息传递给Crystal Reports的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆