如何在两个日期之间使用C#.net在Crystal报表中显示数据? [英] how to show data in crystal report using C# .net between 2 two dates?

查看:131
本文介绍了如何在两个日期之间使用C#.net在Crystal报表中显示数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我....我想在从datetimepicker1和datetimepicker2选择的两个日期之间生成报告...此代码使问题出现,如果我选择2个日期,它将显示数据直到倒数第二个日期. ..就像我选择介于17到20之间的日期一样,它显示的是从日期17到日期19的数据. 数据库中列date的数据类型为datetime....

kindly help me....i want to generate report between two dates selected from datetimepicker1 and datetimepicker2.....with this code m getting the problem that if i select 2 dates it show data till second last date....like if i select date between 17 to 20 it show data from date 17 to date 19.....help me plz
the datatype of column date in database is datetime....

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace cargo
{
    public partial class Employee_Information : Form
    {
        SQLConnection db = new SQLConnection();
        public Employee_Information()
        {
            InitializeComponent();
        }

       

        private void Employee_Information_Load(object sender, EventArgs e)
        {
            db.DBConnect();
        }

        private void Employee_Information_FormClosed(object sender, FormClosedEventArgs e)
        {
            db.DBClose();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            try
            {
              string fromdate = Convert.ToDateTime(dateTimePicker1.Text).ToString("dd-MMM-yyyy HH:mm:ss");
              string todate = Convert.ToDateTime(dateTimePicker2.Text).ToString("dd-MMM-yyyy HH:mm:ss");
                string str1 = "select * from employeeinfo where Date between '" + fromdate+ "' and  '" + todate + "' ";
                db.cmd.Connection = db.conn;
                db.cmd.CommandText = str1;
                SqlDataAdapter da = new SqlDataAdapter(str1, db.conn);
                da.Fill(db.ds, "employeeinfo ");
                CrystalReport1 cr = new CrystalReport1();
                cr.SetDataSource(db.ds.Tables[0]);
                crystalReportViewer1.ReportSource = cr;

            }
            catch (Exception ex)
            {
                MessageBox.Show("error in query" + ex.Message);
            }

        }

       
    }
}

推荐答案

你好

看看这个

我有一个名为TestTable的表,它有很多数据
Hello

just see this

I have table that names TestTable and it has these many data
1	Tejas	2011-09-19 14:54:11.613
2	Chirag	2011-09-12 14:54:11.633
3	Ankit	2011-09-23 14:54:11.633
4	Sagar	2011-09-15 14:54:11.633
5	Hiren	2011-09-28 14:54:11.633



现在,如果我触发此查询,它将导致此



now if i fire this query it will result this

SELECT * FROM TestTable WHERE date between '2011-09-12' and '2011-09-19' ORDER BY date

2	Chirag	2011-09-12 14:54:11.633
4	Sagar	2011-09-15 14:54:11.633



但实际上还有一条记录是2011年9月19日的名称为tejas的记录,
实际上,它会在上面的查询结果中出现,但实际上不存在

为什么?

因为列date的数据类型为datetime,所以它将以hh:mm:ss:tt格式存储带有时间的日期以进行检查,您可以触发此查询



but in actual there is also a record with name as tejas with 2011-09-19 date,
actually it will be there in the above query result but it is not there

why???

because the column date hase data type as datetime so it will store a date with time in hh:mm:ss:tt format for cheking you can fire this query

print cast('2011-09-12' as datetime)

result is 

Sep 12 2011 12:00AM



因此,如果我们希望在查询中看到2011-09-19记录,我们也要给时间,但是该时间大于插入记录的时间,例如2011-09-19有时间插入次数是14:54:11.613
因此,要获取该记录,我们必须给14:55或15:00之类的时间

看到这个查询是..



so if we want the 2011-09-19 record will be seen in the query we want to give time also, but that time is greater then the record''s time when it is inserted like for 2011-09-19 has the time of insertion is 14:54:11.613
so for getting that record we have to give a time like 14:55 or 15:00

see this query is..

SELECT * FROM TestTable WHERE date between '2011-09-12' and '2011-09-19 14:55' ORDER BY date

result is 

2	Chirag	2011-09-12 14:54:11.633
4	Sagar	2011-09-15 14:54:11.633
1	Tejas	2011-09-19 14:54:11.613



现在您了解了为什么无法在查询中获得期望的结果..
希望这对您有所帮助...并且不要忘记接受该解决方案并对它进行评分..



now you understand why you cant get the desired result in your query..
hope this will help full to you... and don''t forgot to accept the solution and also rate it..


这篇关于如何在两个日期之间使用C#.net在Crystal报表中显示数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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