求教水晶报表参数查询的问题
我在vb.net中作了一个水晶报表的程序,在不加任何参数的情况下,可以正常运行,现在我想设定三个参数进行查询,我在Form上加入了一个文本框txtyear用于输入年份,两个ComBox,一个ComBMonth用于选择月份,另一个ComBDay用于选择日期,我在按钮中加入如下的代码:
If Len(txtyear.Text) = 0 Then
MsgBox("请选择年份", MsgBoxStyle.Critical, "警告...")
Return
End If
If CombMonth.SelectedIndex < 0 Then
MsgBox("请选择月份", MsgBoxStyle.Critical, "警告...")
Return
End If
If ComBDay.SelectedIndex < 0 Then
MsgBox("请选择日期", MsgBoxStyle.Critical, "警告...")
Return
End If
'创建一个内含链接信息的数据库链接对象
Dim myConnection As New SqlConnection
myConnection.ConnectionString = "Persist Security Info=False;Integrated Security=SSPI;database=cw;server=ysony;Connect Timeout=30"
'声明一个sqlDataadapter类型的变量
Dim MyCommand As SqlDataAdapter
'创建数据集对象
Dim ds As DataSet = New dsDayWater
'创建一个SqlDataAdapter对象
MyCommand = New SqlDataAdapter("select stcd,tm,z from tb_river_r where datepart(yy,tm)=@txtyear and datepart(mm,tm)=@cobmonth and datepart(dd,tm)=@cobday ", myConnection)
'设置select命令语法中的参数的数据类型
MyCommand.SelectCommand.Parameters.Add(New SqlClient.SqlParameter("@txtyear", SqlDbType.Char, 8))
MyCommand.SelectCommand.Parameters.Add(New SqlClient.SqlParameter("@cobmonth", SqlDbType.Int, 4))
MyCommand.SelectCommand.Parameters.Add(New SqlClient.SqlParameter("@cobday", SqlDbType.Int, 4))
'将所选择参数指派给select命令
MyCommand.SelectCommand.Parameters("@txtyear").Value = txtyear.Text.ToString()
MyCommand.SelectCommand.Parameters("@cobmonth").Value = CombMonth.SelectedItem.ToString()
MyCommand.SelectCommand.Parameters("@cobday").Value = ComBDay.SelectedItem.ToString()
'连接到数据库,提取数据,然后中断连接
MyCommand.Fill(ds, "tb_river_r")
'声明并创建一个报表对象
Dim myrepdoc As New ReportDocument
'载入报表
myrepdoc.Load(Application.StartupPath & "\Crdaywater.rpt")
'将数据集传递给报表对象
myrepdoc.SetDataSource(ds)
'邦定报表查看器对象
CrtViewerWater.ReportSource = myrepdoc
表tb_river_r中的几个字段类型分别为:
stcd(cha(8),not null),tm(datatime,not null),z(float)
这段代码编译通过,在运行时出现错误.请帮我看看错在那儿,谢谢!
If Len(txtyear.Text) = 0 Then
MsgBox("请选择年份", MsgBoxStyle.Critical, "警告...")
Return
End If
If CombMonth.SelectedIndex < 0 Then
MsgBox("请选择月份", MsgBoxStyle.Critical, "警告...")
Return
End If
If ComBDay.SelectedIndex < 0 Then
MsgBox("请选择日期", MsgBoxStyle.Critical, "警告...")
Return
End If
'创建一个内含链接信息的数据库链接对象
Dim myConnection As New SqlConnection
myConnection.ConnectionString = "Persist Security Info=False;Integrated Security=SSPI;database=cw;server=ysony;Connect Timeout=30"
'声明一个sqlDataadapter类型的变量
Dim MyCommand As SqlDataAdapter
'创建数据集对象
Dim ds As DataSet = New dsDayWater
'创建一个SqlDataAdapter对象
MyCommand = New SqlDataAdapter("select stcd,tm,z from tb_river_r where datepart(yy,tm)=@txtyear and datepart(mm,tm)=@cobmonth and datepart(dd,tm)=@cobday ", myConnection)
'设置select命令语法中的参数的数据类型
MyCommand.SelectCommand.Parameters.Add(New SqlClient.SqlParameter("@txtyear", SqlDbType.Char, 8))
MyCommand.SelectCommand.Parameters.Add(New SqlClient.SqlParameter("@cobmonth", SqlDbType.Int, 4))
MyCommand.SelectCommand.Parameters.Add(New SqlClient.SqlParameter("@cobday", SqlDbType.Int, 4))
'将所选择参数指派给select命令
MyCommand.SelectCommand.Parameters("@txtyear").Value = txtyear.Text.ToString()
MyCommand.SelectCommand.Parameters("@cobmonth").Value = CombMonth.SelectedItem.ToString()
MyCommand.SelectCommand.Parameters("@cobday").Value = ComBDay.SelectedItem.ToString()
'连接到数据库,提取数据,然后中断连接
MyCommand.Fill(ds, "tb_river_r")
'声明并创建一个报表对象
Dim myrepdoc As New ReportDocument
'载入报表
myrepdoc.Load(Application.StartupPath & "\Crdaywater.rpt")
'将数据集传递给报表对象
myrepdoc.SetDataSource(ds)
'邦定报表查看器对象
CrtViewerWater.ReportSource = myrepdoc
表tb_river_r中的几个字段类型分别为:
stcd(cha(8),not null),tm(datatime,not null),z(float)
这段代码编译通过,在运行时出现错误.请帮我看看错在那儿,谢谢!
浙公网安备 33010602011771号