从Access数据库中获取日期X和日期Y之间的记录? [英] Getting records from between Date X and Date Y from my Access database?

查看:109
本文介绍了从Access数据库中获取日期X和日期Y之间的记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Access数据库中有一个名为Recharge的表.

I have a table called Recharge in a Access Database.

其中一些字段是RechargeDate,Account,Number等.

Some of the fields are RechargeDate, Account, Number, etc.

我想检索两个日期之间的所有记录,所以我编写了以下查询:

I wanted to retrieve all records between two dates so I wrote the following query:

string Query = "select * from Recharge where Account='" + comboBox1.Text + "' 
and RechargeDate between '"+dateTimePicker1.Value.Date.ToShortDateString()+"' and '"+dateTimePicker2.Value.Date.ToShortDateString()+"'"

查询运行正常,但是我遇到的唯一问题是我只能从一个月中检索日期.

The query runs fine but the only problem I've run in to is that I am only able to retrieve dates from a single Month.

如果我请求一个跨度超过一个月的记录,则不会得到正确的结果.

If I request records from a span that encompasses more than a single month, I do not get the proper result.

有帮助吗?

查询执行得很好,但是问题是我只能在一个月的日期之间获取详细信息,如果开始月份和结束月份不同,我将无法获得正确的结果,请帮助我

the query executes nice but the problem is i can only able to get details between the dates in single month, if the starting month and ending month differs i cant get the proper result plzz help me

推荐答案

也许您遇到了一些字符串日期格式问题;尝试使用以下代码:

Maybe you're running into some string date format issue; try with this code:

OleDbCommand command = new OleDbCommand(
    "SELECT * FROM Recharge " + 
    "WHERE Account=@Account and " + 
    "RechargeDate between @RechargeDateStart AND @RechargeDateEnd");
command.Parameters.AddWithValue("@Account", comboBox1.Text);
command.Parameters.AddWithValue("@RechargeDateStart",dateTimePicker1.Value.Date);
command.Parameters.AddWithValue("@RechargeDateEnd"  ,dateTimePicker2.Value.Date);

顺便说一句,您不应尝试连接SQL命令,因为这可能导致SQL注入攻击.

BTW, you shouldn't try to concatenate SQL commands, as this can lead to SQL injection attacks.

这篇关于从Access数据库中获取日期X和日期Y之间的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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