[英]Error: Conversion failed when converting date and/or time from character string
How can I solve this error? 我该如何解决这个错误?
Conversion failed when converting date and/or time from character string
从字符串转换日期和/或时间时转换失败
My SQL code : 我的SQL代码:
SELECT
RTRIM(invoiceNo) as [Order No.],
RTRIM(InvoiceDate) as [Order Date],
RTRIM(SubTotal) as [SubTotal],
RTRIM(VATPer) as [Vat+ST %],
RTRIM(VATAmount) as [VAT+ST Amount],
RTRIM(DiscountPer) as [Discount %],
RTRIM(DiscountAmount) as [Discount Amount],
RTRIM(GrandTotal) as [Grand Total],
RTRIM(TotalPayment) as [Total Payment],
RTRIM(PaymentDue) as [Payment Due]
FROM
Invoice_Info
WHERE
InvoiceDate BETWEEN @d1 AND @d2
ORDER BY
InvoiceDate desc
And I call this using a SqlCommand
from C#, adding these parameters: 我使用来自C#的
SqlCommand
调用此命令,并添加以下参数:
cmd.Parameters.Add("@d1", SqlDbType.DateTime, 30, "InvoiceDate").Value = dtpInvoiceDateFrom.Value.Date;
cmd.Parameters.Add("@d2", SqlDbType.DateTime, 30, "InvoiceDate").Value = dtpInvoiceDateTo.Value.Date;
I think that you must remove the ".Date" from the values, because DateTimePicker.Value.Date is returning only the date part of the field. 我认为您必须从值中删除“ .Date”,因为DateTimePicker.Value.Date仅返回字段的日期部分。
Also you can change the parameters to the following: 您也可以将参数更改为以下内容:
cmd.Parameters.Add("@d1", SqlDbType.DateTime).Value = dtpInvoiceDateFrom.Value;
cmd.Parameters.Add("@d2", SqlDbType.DateTime).Value = dtpInvoiceDateTo.Value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.