我在C#中使用SQL Server查询并使用RDLC报告显示查询结果。我的查询在SQL Server上运行时产生正确的结果,但是我收到错误
从字符串转换日期/时间时转换失败
从C#代码中使用它时。我无法弄清楚为什么会出现这个错误。请帮我
C#代码:
DateTime Date =DateTime.Parse(d2.Value.Date.ToString());
string format = "yyyy-MM-dd";
MessageBox.Show(Date.ToString(format));
this.soldItemsDetailsTableAdapter.Fill(this.Daily_Sales_Report.SoldItemsDetails, Date.ToString(format));
该错误发生在此代码的最后一行。
以“yyyy-MM-dd
”格式存储在数据库中的日期和上面的代码根据需要生成日期格式,如下面的代码所确认:
MessageBox.Show(Date.ToString(format));
SQL Server查询:
SELECT
b.CompanyID, b.Category, a.ModelNo, a.ProductCode, a.Qty
FROM
(SELECT
s.DateSold, p.ProductID, p.ModelNo, p.ProductCode,
SUM(sipl.SubTotal) AS Qty
FROM
SalesInvoiceProductsList AS sipl
INNER JOIN
SalesInvoice AS s ON sipl.SalesInvoiceID = s.SalesInvoiceID
INNER JOIN
Product AS p ON sipl.ProductID = p.ProductID
GROUP BY
p.ModelNo, p.ProductCode, p.ProductID, s.DateSold) AS a
INNER JOIN
(SELECT DISTINCT
p.ProductID, p.CompanyID, cat.Category
FROM
Product AS p
INNER JOIN
Category AS cat ON p.CatID = cat.CatID
INNER JOIN
Company AS c ON p.CompanyID = c.CompanyID) AS b ON a.ProductID = b.ProductID
WHERE
(a.DateSold = @Date)
ORDER BY
b.CompanyID
如果我使用简单的查询,它也需要date
输入而不是这个复杂的查询,然后得到没有错误。但是,他的复杂查询在SQL Server上运行良好。
我通过更改除查询之外的任何内容来解决此问题。两个查询在服务器上执行良好,但第一个查询的集成发出了正在讨论的错误,但以下查询不会发出任何错误,尽管两个查询中的日期格式相同。我无法理解原因。谁能解释为什么?
SELECT com.CompanyID AS company, c.Category, p.ModelNo AS Model, (sipl.SubTotal) AS Qty
FROM SalesInvoiceProductsList AS sipl
INNER JOIN SalesInvoice AS s ON sipl.SalesInvoiceID = s.SalesInvoiceID
INNER JOIN Product AS p ON sipl.ProductID = p.ProductID
INNER JOIN Company AS com ON p.CompanyID = com.CompanyID
INNER JOIN Category AS c ON p.CatID = c.CatID
WHERE (s.DateSold = @Date)
GROUP BY com.CompanyID, c.Category, p.ModelNo,sipl.SubTotal