简体   繁体   English

有什么方法可以从 SQLDatabase C# 检索一年中任何一个月的所有记录

[英]Is there any way that l can retrieve all record of any month of the year from SQLDatabase C#

I have succeeded in getting all record of sales transaction for today on just clicking a button.只需单击一个按钮,我就成功地获取了今天的所有销售交易记录。 Below is my codes下面是我的代码

con.Open();
string query = "select Date Transactioncode, Productname, Total FROM Stock WHERE Date >= CAST(GETDATE() AS DATE) ORDER BY Date DESC";
SqlDataAdapter SDA = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
SDA.Fill(dt);
dataGridView1.DataSource = dt;
con.Close(); 

The problem I am having is how to get all sales transaction for the month or any month and year that i wish to search.我遇到的问题是如何获取我希望搜索的月份或任何月份和年份的所有销售交易。 I am using 2 comboBoxes to select month and year in order to get records from the SQLDatabase.我正在使用 2 个组合框到 select 月份和年份,以便从 SQLDatabase 获取记录。 Below the codes i had tried which kept producing error.在我尝试过的代码下方,它不断产生错误。

con.Open();
string query = "SELECT * FROM Stock WHERE Month(Date) = '"+comboMonth.Text+"' AND Year(Date) = '"+comboYear.Text+"'";
SqlDataAdapter SDA = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
SDA.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();

this is the error i got: Conversion failed when converting the varchar value 'January' to data type int"这是我得到的错误:将 varchar 值“January”转换为数据类型 int 时转换失败”

kindly help please请帮忙

I would do this on the SQL side, as there are functions there to render date in any form, which you can compare to.我会在 SQL 端执行此操作,因为那里有以任何形式呈现日期的功能,您可以进行比较。 It might not make for the most efficient query.它可能无法进行最有效的查询。

Either rendering the date as a string would allow use of 'like' and an index (I always liked the ISO YYYY-MM-DD HH:MM:SS.ssssss), or indexing the function expression is also allowed now.将日期呈现为字符串将允许使用“like”和索引(我一直喜欢 ISO YYYY-MM-DD HH:MM:SS.ssssss),或者现在也允许索引 function 表达式。

For just one year one month, you should calculate the first and last second/day of date-times and do a range scan on the date-time value, which is both more efficient and non-hash (ordered) index friendly.对于仅仅一年一个月,您应该计算日期时间的第一和最后一秒/天,并对日期时间值进行范围扫描,这既更有效又对非散列(有序)索引友好。 Some Date types are of day granularity, some are date-time.一些日期类型是日粒度的,一些是日期时间的。 https://docs.microsoft.com/en-us/sql/t-sql/functions/date-and-time-data-types-and-functions-transact-sql?view=sql-server-ver15 And of course the exact ends of days vary with Daylight Savings and the like, a real mess doing queries by shift on 3 shifts a day! https://docs.microsoft.com/en-us/sql/t-sql/functions/date-and-time-data-types-and-functions-transact-sql?view=sql-server-ver15当然确切的结束日期因夏令时等而异,每天 3 班轮班查询真是一团糟!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Convert.ToDateTime,以任何方式描述C#的月份和年份字段? - Convert.ToDateTime , any way to depict month and year fields C#? C# 如何从数据库中检索所有记录 - C# how can i retrieve all record from the database 如何从SQL数据库中检索记录匹配的MemberID,月份和年份 - How to retrieve record match memberID, month and year from sql database 是否有使用 c# wpf 从数据库中检索和上传图像的最短方法? - Is there any shortest way to retrieve and upload image from database using c# wpf? 从SqlDatabase到ZipFile的C#Byte []和fileName - C# Byte[] and fileName from SqlDatabase to ZipFile C#:有没有办法轻松查找/更新对象的所有引用? - C#: Is there any way to easily find/update all references to an object? C方法_strtod_l在C#和Java中是否等效? - Is there any equivalent in C# and Java for the C method _strtod_l? 如何在多维数组(C#)中打印全部1个月(和一年) - How to print all of 1 month (and year) in multidimensional array (c#) 从特殊文件夹C#检索所有文件的方法 - Way to retrieve all the files from Special Folder C# 使用C#中的.NET成员身份的任何方式都可以重置密码 - Any way with the .NET membership in C# that I can reset a password
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM