繁体   English   中英

错误:从字符串转换日期和/或时间时转换失败

[英]Error: Conversion failed when converting date and/or time from character string

我该如何解决这个错误?

从字符串转换日期和/或时间时转换失败

我的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

我使用来自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;

我认为您必须从值中删除“ .Date”,因为DateTimePicker.Value.Date仅返回字段的日期部分。

您也可以将参数更改为以下内容:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM