0

我想使用以下 sql 查询从数据库中检索数据,但这不起作用。我正在尝试根据输入日期获取数据。在数据库中,日期格式为 03/03/2013。以下查询是否对问题正确。

 public DataSet OrderByDate(string date1, string date2)
        {


            //  string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Amrit\\Desktop\\Database.accdb ;Persist Security Info=False;";
            DataSet dataSet = new DataSet();
            OleDbConnection oleConn = new OleDbConnection(connString);

            try
            {
                oleConn.Open();
                string sql = "SELECT Order.OrderNumber, (Customer.Title +SPACE(2)+ Customer.CustomerName) as [Customer Name], Customer.CustomerEbayname, Customer.EmailAddress, Customer.PhoneNumber, (Customer.Address1 + SPACE(2) + Customer.Address2 + SPACE(2)+ Customer.City + SPACE(2) + Customer.PostCode + SPACE(2) + Customer.Country) as Address,  Order.ItemPurchased, Order.PurchasedDate, Order.TotalPrice FROM Customer INNER JOIN [Order] ON Customer.[CustomerID] = Order.[CustomerID] WHERE [PurchasedDate] BETWEEN #date1# AND #date2#";                    OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sql, oleConn);
                dataAdapter.Fill(dataSet, "Customer");
            }
            catch (Exception ex)
            {
                MessageBox.Show("An exception has been occured\n" + ex.ToString());
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                oleConn.Close();
            }
            if (dataSet.Tables.Count <= 0)
                return null;
            else
                return dataSet;
        }
4

1 回答 1

1

由于您使用的是 ,请不要用单引号Access将日期括起来。#

SELECT  [Order].[Order]Number, 
        (Customer.Title +SPACE(2)+ Customer.CustomerName) as [Customer Name],
        Customer.CustomerEbayname, 
        Customer.EmailAddress, 
        Customer.PhoneNumber, 
        (Customer.Address1 + SPACE(2) + Customer.Address2 + SPACE(2)+ Customer.City + SPACE(2) + Customer.PostCode + SPACE(2) + Customer.Country) as Address,  
        [Order].ItemPurchased, 
        [Order].PurchasedDate, 
        [Order].TotalPrice 
FROM    Customer 
        INNER JOIN [Order] 
            ON Customer.[CustomerID] = [Order].[CustomerID]  
WHERE   [PurchasedDate]= #" + date + "#
于 2013-03-03T08:44:01.653 回答