我在查询中得到错误“'#'附近的语法错误不正确如何解决它

时间:2014-07-08 08:49:42

标签: c# sql sql-server

我在查询中遇到错误" '#'附近的语法错误不正确如何解决它

代码:

cmd = new SqlCommand("SELECT 
                    (invoiceNo) as [Invoice No],
                    (InvoiceDate) as [Invoice Date],
                    (Sales.CustomerID) as [Customer ID],
                    (CustomerName) as [Customer Name],
                    (GrandTotal) as [Grand Total],
                    (TotalPayment) as [Total Payment],
                    (PaymentDue) as [Payment Due] 
                    from Sales,Customer 
                        where 
                           Sales.CustomerID=Customer.CustomerID 
                        and 
                           InvoiceDate between #" + DateTimePicker2.Text + "# And #" + DateTimePicker1.Text + "# and PaymentDue > 0 
                        order by 
                           InvoiceDate desc", con);

1 个答案:

答案 0 :(得分:3)

如果您使用TSQL,我认为您应该将date作为字符串传递。但是,我不确定您是否需要任何时间部分,您可能需要修改tostring

中的格式
cmd = new SqlCommand("SELECT 
                    (invoiceNo) as [Invoice No],
                    (InvoiceDate) as [Invoice Date],
                    (Sales.CustomerID) as [Customer ID],
                    (CustomerName) as [Customer Name],
                    (GrandTotal) as [Grand Total],
                    (TotalPayment) as [Total Payment],
                    (PaymentDue) as [Payment Due] 
                    from Sales,Customer 
                        where 
                           Sales.CustomerID=Customer.CustomerID 
                        and 
                           InvoiceDate between '" + DateTimePicker2.Value.ToString("yyyyMMdd") + "' 
                        And '" + DateTimePicker1.Value.ToString("yyyyMMdd") + "' and PaymentDue > 0 
                        order by 
                           InvoiceDate desc", con);