用于生成报告的日期和日期 [英] Use to and from date for generate report

查看:82
本文介绍了用于生成报告的日期和日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



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;
                }
            }
        }
    }
}


这篇关于用于生成报告的日期和日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆