用于生成报告的日期和日期

用于生成报告的日期和日期

问题描述:

亲爱的所有人,



i我在C#中使用userform并使用MS访问数据库我想知道如何在报表查看器中添加日期参数来生成来自和的报表到目前为止PLZ帮助我............................................. .................................................. .....



我的尝试:



visual studio 2010 ................................................ .................................................. ...............

Dear ALL,

i am making userform in C# and using MS access database i want know how can be add date parameter in report viewer in to generate report from and to date plz help me....................................................................................................

What I have tried:

visual studio 2010................................................................................................................

您不需要将日期参数传递给报表查看器,而是将这些参数用作过滤器在您的SQL查询中过滤日期然后填充该输出的数据表

最后将该数据表作为数据源附加到您的报表查看器



这里是我试过的示例代码

Date1和Date2是参数对象(表单上的文本框或日历对象)



You don't need to pass date parameters to report viewer , instead use those parameters as filter in your SQL query to filter the date and then populate data table for that output
finally attach that data table as data source to your report viewer

here is the sample code I tried
Date1 and Date2 are parameter object (text box or calendar objects on form)

private void Form1_Load(object sender, EventArgs e)
{
    Customers dsCustomers = GetData();
    ReportDataSource datasource = new ReportDataSource("Customers", dsCustomers.Tables[0]);
    this.reportViewer1.LocalReport.DataSources.Clear();
    this.reportViewer1.LocalReport.DataSources.Add(datasource);
    this.reportViewer1.RefreshReport();
}
 
private Customers GetData()
{
    string constr = @"Data Source=.\Sql2005;Initial Catalog=Northwind;Integrated Security = true";
    using (SqlConnection con = new SqlConnection(constr))
    {
        // NO! Bad idea. Google "SQL Injection".
        // using (SqlCommand cmd = new SqlCommand("SELECT TOP 20 * FROM customers where Transdate between" & Date1.value & " AND " & Date2.value))
       
        using (SqlCommand cmd = new SqlCommand("SELECT TOP 20 * FROM customers where Transdate between @Date1 AND @Date2"))
        {
            cmd.Parameters.AddWithValue("@Date1", Date1.Value);
            cmd.Parameters.AddWithValue("@Date2", Date2.Value);
            
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (Customers dsCustomers = new Customers())
                {
                    sda.Fill(dsCustomers, "DataTable1");
                    return dsCustomers;
                }
            }
        }
    }
}