当用户在日期之间选择时,不在gridview中显示数据

时间:2018-01-19 05:36:26

标签: c# sql winforms ms-access

 OleDbConn.ole = "SELECT InvoiceDate,InvoiceNo,Customer,SalesPerson,TotalAmount"
 +" FROM Invoice WHERE InvoiceDate BETWEEN "
 + startDate.ToString("yyyy-MM-dd") 
 + " AND " + endDate.ToString("yyyy-MM-dd")
 + " AND (InvoiceNo LIKE '%" + txtSearch.Text 
 + "%' OR SalesPerson LIKE '%"+ txtSearch.Text 
 + "%' OR Customer LIKE '%" + txtSearch.Text + "%') GROUP BY InvoiceNo,InvoiceDate,Customer,SalesPerson,TotalAmount " 
 +"ORDER BY InvoiceDate, InvoiceNo DESC"; //here is my search query

dgw.Rows.Add(
     Convert.ToDateTime(OleDbConn.dr[0]).ToString("MM/dd/yyyy"), 
     OleDbConn.dr[1].ToString(),OleDbConn.dr[2].ToString(),
     OleDbConn.dr[3].ToString(), 
     Strings.FormatNumber(OleDbConn.dr[4]).ToString(), 
     Strings.FormatNumber(OleDbConn.dr[4]).ToString()); //adding data to gridview

当用户选择它应该在gridview中显示数据之间的日期但是没有时,代码在sql中运行良好但在ms-access中失败

3 个答案:

答案 0 :(得分:0)

为什么要将日期比较为stringS?使用参数化,并在比较日期时使用<>代替Between;你应该使用这样的东西:

OleDbConn.ole  = " Query-Here WHERE InvoiceDate > @startDate AND" + 
                 " InvoiceDate < @endDate AND rest-of-code-here" 

现在将值作为参数传递

答案 1 :(得分:0)

你可以试试这个。您应该在静态日期值之前和之后添加#,日期格式应该类似于MM/dd/yyyy

OleDbConn.ole = "SELECT InvoiceDate, InvoiceNo, Customer, SalesPerson, TotalAmount" 
    + " FROM Invoice WHERE InvoiceDate BETWEEN #" 
    + startDate.ToString("MM/dd/yyyy") + "# AND #" 
    + endDate.ToString("MM/dd/yyyy") 
    + "# AND (InvoiceNo LIKE '%" + txtSearch.Text 
    + "%' OR SalesPerson LIKE '%" + txtSearch.Text 
    + "%' OR Customer LIKE '%" + txtSearch.Text + "%') GROUP BY InvoiceNo,InvoiceDate,Customer,SalesPerson,TotalAmount " 
    + "ORDER BY InvoiceDate, InvoiceNo DESC"; //here is my search query

答案 2 :(得分:0)

你试过这个吗?

 OleDbConn.ole = "SELECT InvoiceDate,InvoiceNo,Customer,SalesPerson,TotalAmount"
 +" FROM Invoice WHERE CDate(InvoiceDate) BETWEEN #"
 + startDate.ToString("yyyy-MM-dd") 
 + "# AND #" + endDate.ToString("yyyy-MM-dd")
 + "# AND (InvoiceNo LIKE '%" + txtSearch.Text 
 + "%' OR SalesPerson LIKE '%"+ txtSearch.Text 
 + "%' OR Customer LIKE '%" + txtSearch.Text + "%') GROUP BY InvoiceNo,InvoiceDate,Customer,SalesPerson,TotalAmount " 
 +"ORDER BY InvoiceDate, InvoiceNo DESC";