[英]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.