在 C# 中的报表查看器中加载日期之间数据的方法

Way to load data between dates in report viewer in C#

我试图在 windows 表单的报表查看器控件中获取两个日期之间的数据。过滤报告数据,所以我修改数据集使用参数为

select SN,invoice_date,product_code........ where invoice_date >= @date1 and invoice_date <= @date2

我也试过了

select SN,invoice_date,product_code........ where invoice_date between @date1 and @date2

但是查询没有返回任何数据,我在SQL服务器中使用的数据类型是日期,我将数据集@date1和@date2的属性更改为日期。所以问题是我正在使用的日期时间选择器不起作用,因为它正在为值添加时间。我尝试将 datetimepicker 验证为

datetimepicker1.value.date.toshortdatestring()   even tried  datetimepicker1.value.tostring()

但问题是,它会产生system.datetime cannot be convert into string 的错误。我什至在数据集属性中将 @date1 和 @date2 的数据类型更改为 varchar,但仍然没有加载。

所以这是尝试过的代码

this.Sales_InvoiceTableAdapter.FillByget(this.ProductREP.Sales_Invoice, ProductFrom.Value, ProductTo.Value, txtproductcode.Text);

this.Sales_InvoiceTableAdapter.FillByget(this.ProductREP.Sales_Invoice, ProductFrom.Value.Date, ProductTo.Value.Date, txtproductcode.Text);

我错过了什么?我在 SQL 服务器上使用 C#。

假设 Sales_InvoiceTableAdapter.FillByget 的签名是 FillByget(string invoice, DateTime from, DateTime to, string productCode),那么您应该不需要转换为字符串,您应该检查是否正确设置了时间部分。

this.Sales_InvoiceTableAdapter.FillByget(this.ProductREP.Sales_Invoice, ProductFrom.Value.Date, ProductTo.Value.Date.AddDays(1), txtproductcode.Text);

给定 ProductFrom.ValueProductTo.Value 等于 2020-04-28T10:55:00,上面的代码将导致 ProductFrom.Value = 2020-04-28T00:00:00ProductTo.Value = 2020-04-29T00:00:00
如果您只想获取特定日期(不包括午夜)的值,请从最终 to 值中减去 1 秒。例如ProductTo.Value.Date.AddDays(1).Subtract(TimeSpan.FromSeconds(1)).