使用 C# 查询 EXCEL 日期

发布于 2024-09-13 03:20:22 字数 769 浏览 4 评论 0原文

我试图使用 C# 中的查询制作 Excel 图表,我需要收集上个月的数据。我正在使用以下代码,它没有给出任何错误,但也没有给出任何结果。

基本上我有 Excel 表格中的数据,并使用这些数据制作图表。

首先,我获取两个日期并将它们转换为短字符串,然后将字符串与从 Excel 中以短强格式选取的日期进行匹配。

如果有人能回答,我将非常感谢帮助。

谢谢,

代码:

// Get current date and time
DateTime dtx = DateTime.Now;
string Date = dtx.ToShortDateString();

// Calculating the last month's date (substracting days from current date.)
DateTime lastmonth= DateTime.Today.AddDays( -30 );
string Date2 = lastmonth.ToShortDateString();

OleDbCommand objCmdSelect = new OleDbCommand(
"SELECT [Hour],(Format(Date, 'Short Date')) AS text_Date,[U_CSSR] FROM [" + excelSheets[j] + "] WHERE CI=" + id + " AND (Format(Date, 'Short Date'))BETWEEN "+ Date + " AND "+ Date2 + " ", objConn);

I was trying to make excel graphs using query in c# and I need to collect data for the last month. I am using the following code and its not giving any error but its not giving any result either.

Basicaly I have the data in excel sheets and using that data im making graphs.

First im geting the two dates and converting them to short string and then im matching the strings with the dates picked from excel in the short strong format.

If anyone could answer, I would really appreciate help.

Thankyou,

THE CODE:

// Get current date and time
DateTime dtx = DateTime.Now;
string Date = dtx.ToShortDateString();

// Calculating the last month's date (substracting days from current date.)
DateTime lastmonth= DateTime.Today.AddDays( -30 );
string Date2 = lastmonth.ToShortDateString();

OleDbCommand objCmdSelect = new OleDbCommand(
"SELECT [Hour],(Format(Date, 'Short Date')) AS text_Date,[U_CSSR] FROM [" + excelSheets[j] + "] WHERE CI=" + id + " AND (Format(Date, 'Short Date'))BETWEEN "+ Date + " AND "+ Date2 + " ", objConn);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泡沫很甜 2024-09-20 03:20:22

我认为你的 WHERE 子句在逻辑上是不正确的。它应该是

... BETWEEN "+ Date2 + " AND "+ Date ...

较早的日期应该排在前面。BETWEEN

a AND b 等于:x > a 且 x < b。

I think your WHERE clause is logically incorrect. it should be

... BETWEEN "+ Date2 + " AND "+ Date ...

The earlier date should come first.

BETWEEN a AND b is equal to: x > a and x < b.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文