文章出处:http://mnky.blogchina.com/mnky/6721026.html
ADO.NET 连接水晶报表和存储过程
关键词: ADO.NET 水晶报表 存储过程 数据库 asp.net
存储过程代码如下:
CREATE procedure dbo.dt_getbyCry @content char (10), @curyear int, @curmonth int as set nocount on if (@curyear = 0) begin raiserror('Must specify a year.',-1,-1) return (1) end if (@content="0") begin if (@curmonth = 0) begin select content,curtime from dbo.t_codeinfo where Year(curtime)=@curyear end else begin select content,curtime from dbo.t_codeinfo where Year(curtime)=@curyear and Month(curtime)=@curmonth end end else begin if (@curmonth = 0) begin select content,curtime from dbo.t_codeinfo where content=@content and Year(curtime)=@curyear end else begin select content,curtime from dbo.t_codeinfo where content=@content and Year(curtime)=@curyear and Month(curtime)=@curmonth end end GO
由于使用了存储过程,使得水晶报表无法使用 Pull 模式(直接连接数据库),而需要使用 dataset 接收数据,使用水晶报表的 Push 模式。
使用 Push 模式的过程如下:
1、设计一个 dataset: 右击“解决方案浏览器”,选择“添加”-->“添加新项”-->“数据集”-->取名 ds1; 从“服务器资源管理器”中拖放一个表,此时在数据集中就会有一个表(如 t_crystal)的结构图; 若该表字段和存储过程输出不统一(在上一步中不能直接拖拽存储过程),可以修改这个结构图; 这个数据集保存在 .xsd 文件中,其仅包含一个结构图,里面不会有任何数据。 2、创建 .rpt 文件: 创建一个报表文件(如 CryShow),右击“详细资料”-->“添加/删除数据库”; “数据库专家”窗口-->“项目数据”-->“ado.net数据集”-->“ds1”; 将“t_crystal”表添加到“选定的表”中。 3、实例化 dataset 和 rpt 报表,并与显示控件建立联系。
调用存储过程的方式有两种:
第一种方式,是直接通过 SqlDataAdapter 调用存储过程,过程名称后面接参数。但这种方式执行效率不高。
Dim da As SqlClient.SqlDataAdapter strSql = "dt_getbyCry '" + type_drop.SelectedValue + "','" + year_drop.SelectedValue + "','" + month_drop.SelectedValue + "'" da = New SqlClient.SqlDataAdapter(strSql, sql_conn)
第二种方式就是建立 SqlCommand:
sql_conn.Open() Dim cmd As SqlClient.SqlCommand ' 填写存储过程名 cmd = New SqlClient.SqlCommand("dt_getbyCry", sql_conn) ' 指明这里使用存储过程 cmd.CommandType = CommandType.StoredProcedure ' 添加参数并赋值 cmd.Parameters.Add("@curyear", SqlDbType.Int) cmd.Parameters.Add("@content", SqlDbType.Char) cmd.Parameters.Add("@curmonth", SqlDbType.Int) cmd.Parameters("@content").Value = type_drop.SelectedValue cmd.Parameters("@curyear").Value = year_drop.SelectedValue cmd.Parameters("@curmonth").Value = month_drop.SelectedValue Dim da As SqlClient.SqlDataAdapter ' 实例化数据集 Dim ds As ds1 ds = New ds1 da = New SqlClient.SqlDataAdapter(cmd) da.Fill(ds, "result") ' 实例化水晶报表 Dim oRpt1 As CryShow oRpt1 = New CryShow oRpt1.SetDataSource(ds.Tables("result")) CrystalReportViewer1.ReportSource = oRpt1 sql_conn.Close()
在水晶报表的使用过程中,如果出现 Load Report Failed 错误,一般是 CrystalReportViewer 的 ReportSource 路径没有设置正确,注意路径中使用双斜 杠“//”或使用 Server.MapPath 函数:
Imports CrystalDecisions.CrystalReports.Engine Dim oRpt As ReportDocument oRpt = New ReportDocument oRpt.Load(Server.MapPath("CryShow.rpt")) oRpt.SetDataSource(ds)
如果出现 Logon Failed 错误,可能需要使用 TableLogOnInfo:
Imports CrystalDecisions.Shared Dim logOnInfo As TableLogOnInfo logOnInfo = New TableLogOnInfo logOnInfo.ConnectionInfo.DatabaseName = "DBName" logOnInfo.ConnectionInfo.ServerName = "ServerName" logOnInfo.ConnectionInfo.UserID = "username" logOnInfo.ConnectionInfo.Password = "password" oRpt.Database.Tables("t_crystal").ApplyLogOnInfo(logOnInfo)