所属分类:.NET技术 图表区
-----------------------------------------
最近因为项目要用到水晶报表,开发环境为:VB.NET+SQL SERVER 2005 +水晶报表
我个人的习惯一般是将报表数据源写成存储过程,然后调用参数。例如打印订购单的时候,我会将选择订购单的记录写成存储过程:Select_PO,以后只要传递参数@Num(订购单号)就可以了。
下面的例子中,首先大家建立一个配置文件:app.config,全部内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<sources>
<!-- This section defines the logging configuration for My.Application.Log -->
<source name="DefaultSource" switchName="DefaultSwitch">
<listeners>
<add name="FileLog"/>
<!-- Uncomment the below section to write to the Application Event Log -->
<!--<add name="EventLog"/>-->
</listeners>
</source>
</sources>
<switches>
<add name="DefaultSwitch" value="Information" />
</switches>
<sharedListeners>
<add name="FileLog"
type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
initializeData="FileLogWriter"/>
<!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
<!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
</sharedListeners>
</system.diagnostics>
<appSettings>
<add key="SqlServer" value="Server=Localhost;DataBase=Test;Uid=sa;Pwd=123456;connect timeout=60;"/>
</appSettings>
</configuration>
----------------------------------------------------------------------
向水晶报表传递参数时,要注意的是:必须向服务器的名称、数据库名、登录名、密码传递给报表文件,如果有子报表的话,这些参数也要传递。
下面是我的代码,各位可能要适当修改。
本文有参考网上的一篇文章,但是哪位的我就记不得了,请原作者不要见怪。
Try
Me.Cursor = Cursors.WaitCursor
Dim Num, strRPTFile, str() As String
Dim srv, db, user, pwd As String
Dim sql As String
Dim dsSub As New DataSet
Dim sqlConn As New SqlConnection
If Me.dgrd1.DataSource Is Nothing Then
Exit Function
End If
If Me.dgrd1.SelectedCells.Count <= 0 Then
Exit Function
End If
If Me.dgrd1.CurrentCell Is Nothing Then
Exit Function
End If
Num = Me.dgrd1.Rows(Me.dgrd1.CurrentRow.Index).Cells("Num").Value
If Num Is Nothing Then
Exit Function
Else
str = ConfigurationManager.AppSettings("SqlServer").Split(";")
strRPTFile = GetReportPath("PUR", "PUR_PO.rpt")
For I As Integer = 0 To str.Length - 1
If str(I).IndexOf("=") > 0 Then
If str(I).Split("=")(0).ToUpper = "Server".ToUpper Then
srv = str(I).Split("=")(1)
End If
If str(I).Split("=")(0).ToUpper = "DataBase".ToUpper Then
db = str(I).Split("=")(1)
End If
If str(I).Split("=")(0).ToUpper = "Uid".ToUpper Then
user = str(I).Split("=")(1)
End If
If str(I).Split("=")(0).ToUpper = "Pwd".ToUpper Or str(I).Split("=")(0).ToUpper = "Password".ToUpper Then
pwd = str(I).Split("=")(1)
End If
End If
Next
Dim CMD As System.Data.SqlClient.SqlCommand
Dim sqlAD As New SqlDataAdapter
Dim ds As New DataSet
Dim myReportDoc As New CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim myParameterValues As New CrystalDecisions.Shared.ParameterValues
Dim myDiscreteValue As New CrystalDecisions.Shared.ParameterDiscreteValue
strRPTFile = GetReportPath("PUR", "PUR_PO.rpt")
sqlConn.ConnectionString = ConfigurationManager.AppSettings("SqlServer")
If sqlConn.State = ConnectionState.Closed Then
sqlConn.Open()
End If
CMD = New SqlClient.SqlCommand("Select_PO", sqlConn)
CMD.CommandType = CommandType.StoredProcedure
CMD.CommandTimeout = 60
CMD.Parameters.Add(New SqlClient.SqlParameter("@Num", SqlDbType.NVarChar, 4000))
CMD.Parameters("@Num").Value = Num
CMD.ExecuteNonQuery()
CMD.Dispose()
sqlAD.SelectCommand = CMD
sqlAD.Fill(ds, "Rptds")
’向子報表傳遞參數
sql = "exec dbo.GetCommpanyInfo"
dsSub = ReturnDataset(sql)
Dim logonInfo As New TableLogOnInfo
Dim table As Table
myReportDoc.Load(strRPTFile)
’设置登录信息
For Each table In myReportDoc.Database.Tables
logonInfo = table.LogOnInfo
With logonInfo.ConnectionInfo
.ServerName = srv
.DatabaseName = db
.UserID = user
.Password = pwd
End With
table.ApplyLogOnInfo(logonInfo)
Next
’向子報表傳遞登錄參數
For I As Integer = 0 To myReportDoc.Subreports.Count - 1
For Each table In myReportDoc.Subreports(I).Database.Tables
With logonInfo.ConnectionInfo
.ServerName = srv
.DatabaseName = db
.UserID = user
.Password = pwd
End With
table.ApplyLogOnInfo(logonInfo)
Next
If myReportDoc.Subreports(I).Name = "CommpanyInfo" Then
myReportDoc.Subreports(I).SetDataSource(dsSub.Tables(0))
End If
Next
myReportDoc.SetDataSource(ds.Tables(0))
My.Forms.Frm_Report.CrystalReportViewer1.ReportSource = myReportDoc
My.Forms.Frm_Report.CrystalReportViewer1.Zoom(1)
My.Forms.Frm_Report.CrystalReportViewer1.DisplayGroupTree = False
Frm_Report.ShowDialog()
End If
Catch ex As Exception
Me.Cursor = Cursors.Default
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "信息提示")
Finally
Me.Cursor = Cursors.Default
End Try
--------------------------------------------------------
写的很好,如果有案例的话就更好了,给我传一份OK?
ruizhang250@163.com