从 vb.net windows 应用程序传递日期范围参数

我需要将日期范围参数从 vb.net 应用程序传递给水晶报告,但我不知道如何去做。对于任何可能想帮助我的人,请基于以下场景:

我有一个从 Oracle 数据库中提取数据的水晶报表。在数据库中,我有一个学生表,每条记录都有出生日期、姓名和姓氏。我只需要在报告上显示出生日期介于 X 和 Y 日期之间的记录,包括两者。日期 X 和日期 Y 是打开报告时在 vb.net 应用程序上传递的日期。我正在使用水晶报表 9、visual studio 2008 和 oracle 9i。

有人请帮助我,我该如何实现?

stack overflow passing date range parameters from a vb.net windows application
原文答案

答案:

作者头像

假设您的 Crystal Report 绑定到 SQL 命令,例如:

SELECT name, surname FROM students WHERE birthdate BETWEEN {?@pDate1} AND {?@pDate2}

在 VB.NET 中,您需要传递如下参数:

Private rptSummary As New ReportDocument()

Protected Sub Page_Init(sender As Object, e As EventArgs)
    Try
        Dim pFields As New ParameterFields()
        Dim pField_Date1 As New ParameterField()
        Dim pField_Date2 As New ParameterField()
        Dim pDiscreteValue_Date1 As New ParameterDiscreteValue()
        Dim pDiscreteValue_Date2 As New ParameterDiscreteValue()

        Dim ApplPath As String = Server.MapPath("MyReport.rpt")
        rptSummary.FileName = ApplPath
        pField_Date1.Name = "@pDate1"
        pField_Date2.Name = "@pDate2"

        Dim BirthDate1, BirthDate2

        ' Parameter: @pDate1 

        pDiscreteValue_Date1.Value = BirthDate1
        pField_Date1.CurrentValues.Add(pDiscreteValue_Date1)
        pFields.Add(pField_Date1)

        ' Parameter: @pDate2 

        pDiscreteValue_Date2.Value = BirthDate2
        pField_Date2.CurrentValues.Add(pDiscreteValue_Date2)
        pFields.Add(pFieldDate2)

        CrystalReportViewer1.ParameterFieldInfo = pFields
        rptSummary.SetParameterValue(0, pDiscreteValue_Date1)
        rptSummary.SetParameterValue(1, pDiscreteValue_Date2)
        CrystalReportViewer1.ReportSource = rptSummary
    Catch ex As Exception

    End Try
End Sub

此代码最初是在 ASP.NET 中使用 C# 编写的。我将其转换为 VB.NET。请更正任何差异。

作者头像

只需创建一个带有所需参数的水晶报表,并在 from 中为 from 日期和 to date 创建按钮、datetimepicker。

插入按钮单击的代码。

完美地为我工作............

如有错误修改..

谢谢问

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim cryRpt1 As New ReportDocument
    Dim str As String = System.AppDomain.CurrentDomain.BaseDirectory
    str = str.Substring(0, str.Length - 10) 
    cryRpt1.Load(str & "daily_scrl_rep.rpt")
    cryRpt1.SetParameterValue(0, DTPFrom.Text)
    cryRpt1.SetParameterValue(1, DTPTO.Text)
    CrystalReportViewer1.ReportSource = cryRpt1
    CrystalReportViewer1.Refresh()
End Sub
作者头像
Imports CrystalDecisions.CrystalReports.Engine  
Imports CrystalDecisions.Shared

Dim crtableLogoninfos As New CrystalDecisions.Shared.TableLogOnInfos()
Dim crtableLogoninfo As New CrystalDecisions.Shared.TableLogOnInfo()
Dim crConnectionInfo As New CrystalDecisions.Shared.ConnectionInfo()
Dim crit As String
Dim CrTables As Tables
Dim CrTable As Table
reportdocument.Load(Server.MapPath("~/Reports/IssueReport.rpt"))
CrTables = reportdocument.Database.Tables
 For Each CrTable In CrTables
     crtableLogoninfo = CrTable.LogOnInfo
    crtableLogoninfo.ConnectionInfo = crConnectionInfo
    CrTable.ApplyLogOnInfo(crtableLogoninfo)
     CrTable.Location = crConnectionInfo.DatabaseName & ".dbo." & CrTable.Location.Substring(CrTable.Location.LastIndexOf(".") + 1)
  Next
 Crit = "{tablename.fieldname}>=#" & Format(CDate(txtfromdate.Text), "yyyy/MM/dd") & "#"
    Crit = Crit & " and {tablename.fieldname} <=#" & Format(CDate(txtto.Text),      "yyyy/MM/dd") & "#"
  reportdocument.RecordSelectionFormula = Crit
    CrystalReportViewer1.ReportSource = reportdocument
    CrystalReportViewer1.RefreshReport()
作者头像

只需 2 行就足够了

rptSummary.SetParameterValue("your report perameter name", your value 1st value)
rptSummary.SetParameterValue("your report perameter name", your value 2nd value)

就是这样。

作者头像
string rang1 = Session["Rang1"].ToString();
            string rang2 = Session["Rang2"].ToString(); ;

            ReportDocument obj = new ReportDocument();
            obj.Load(Server.MapPath("~/CType_CrystalReport.rpt"));
            obj.SetDatabaseLogon("sa", "12qwaszx", "BTS-10", "BTS_ERP");
            ParameterFields paraf = new ParameterFields();
            ParameterField par = new ParameterField();
            ParameterField par2 = new ParameterField();
         par.ParameterFieldName = "CtCode";

            ParameterDiscreteValue dcpara1 = new ParameterDiscreteValue();
            ParameterDiscreteValue dcpara2 = new ParameterDiscreteValue();
            par.Name = rang1;
            par2.Name = rang2;

            dcpara1.Value = rang1.ToString();
            par.CurrentValues.Add(dcpara1);
            paraf.Add(par);

            dcpara2.Value = rang2.ToString();
            par2.CurrentValues.Add(dcpara2);
            paraf.Add(par2);

            CrystalReportViewer1.ParameterFieldInfo = paraf;
            obj.SetParameterValue("CtCode", dcpara1);
            obj.SetParameterValue("CtCode", dcpara2);
            CType_DataSet dsCustomers = GetData("select * from GL_CUSTTYPE where CT_CODE between '" + rang1.ToString() + "' AND '" + rang2.ToString() + "' ");
            obj.SetDataSource(dsCustomers);

            CrystalReportViewer1.ReportSource = obj;
            CrystalReportViewer1.RefreshReport();