我遇到一个问题,即查询构建器返回的数据与查询构建器或Visual Studio 2015上的数据预览显示的数据不同。
我的SQL查询是:
SELECT
COUNT(Date_Order_Despatched.[Order Number]) AS Orders,
CONVERT(varchar(10), DATEADD(DAY, @day * (DATEDIFF(day, @fromdate, Date_Order_Despatched.[Date Order Desptached]) / @day), @fromdate), 20) AS DateDespatched,
(CASE
WHEN scheme.waopheadm.courier_serv_name = 'Generic Next Day'
THEN 'B2B'
WHEN scheme.waopheadm.courier_serv_name = 'Generic Pallet Service'
THEN 'B2B'
ELSE 'B2C'
END) AS B2BorB2C
FROM
Date_Order_Despatched
INNER JOIN
scheme.waopheadm ON Date_Order_Despatched.[Order Number] = scheme.waopheadm.orderkey
WHERE
(Date_Order_Despatched.[Date Order Desptached] BETWEEN @fromdate AND @todate)
GROUP BY
(DATEADD(DAY, @day * (DATEDIFF(day, @fromdate, Date_Order_Despatched.[Date Order Desptached]) / @day), @fromdate)),
(CASE WHEN scheme.waopheadm.courier_serv_name = 'Generic Next Day' THEN 'B2B' WHEN scheme.waopheadm.courier_serv_name = 'Generic Pallet Service' THEN 'B2B' ELSE 'B2C' END)
这是我从这个查询得到的结果:
989 2016-08-01 B2C
575 2016-08-06 B2C
432 2016-08-11 B2C
678 2016-08-16 B2C
870 2016-08-21 B2C
840 2016-08-26 B2C
678 2016-08-31 B2C
3 2016-09-05 B2C
这是具有相同参数的datagridview的结果:
257 01/08/2016
240 02/08/2016
147 03/08/2016
195 04/08/2016
150 05/08/2016
193 08/08/2016
165 09/08/2016
217 10/08/2016
164 11/08/2016
128 12/08/2016
140 15/08/2016
186 16/08/2016
140 17/08/2016
169 18/08/2016
183 19/08/2016
223 22/08/2016
205 23/08/2016
181 24/08/2016
261 25/08/2016
255 26/08/2016
333 29/08/2016
252 30/08/2016
237 31/08/2016
211 01/09/2016
230 02/09/2016
3 05/09/2016
感谢您的任何帮助。
c#代码绑定数据:
using (SqlConnection sqlConnection = new SqlConnection(sqlConnectionString))
{
sqlConnection.Open();
cs3liveDataSet.EnforceConstraints = false;
order_Despatched_ScreeTableAdapter.FillChartPerXDaysAll(cs3liveDataSet.Order_Despatched_Scree,Convert.ToInt16(otherNumericUpDown.Value), Convert.ToDateTime(fromDateTimePicker.Value), Convert.ToDateTime(toDateTimePicker.Value), B2BOrB2CComboBox.Text);
OrderDataGridView.Refresh();
OrderDataGridView.Visible = true;
OrderDataGridView.Enabled = true;
sqlConnection.Close();
}
我重做了代码,以便从sqlcommand中获取:
using (SqlConnection sqlConnection = new SqlConnection(sqlConnectionString))
{
sqlConnection.Open();
SqlCommand cmd = new SqlCommand(@"SELECT COUNT([Order Number]) AS Orders, CONVERT(varchar(10), DATEADD(DAY, (5*(DATEDIFF(day, @FROMDATE, [Date Order Desptached])/5)), @FROMDATE), 103) AS DateDespatched FROM Date_Order_Despatched WHERE ([Date Order Desptached] BETWEEN @FROMDATE AND @TODATE) GROUP BY CONVERT(varchar(10), DATEADD(DAY, (5*(DATEDIFF(day, @FROMDATE, [Date Order Desptached])/5)),@FROMDATE), 103)", sqlConnection);
//cmd.Parameters.AddWithValue("@DAY",otherNumericUpDown.Value);//@DAY
cmd.Parameters.AddWithValue("@FROMDATE", SqlDbType.DateTime).Value = fromDateTimePicker.Value;//@FROMDATE
cmd.Parameters.AddWithValue("@TODATE", SqlDbType.DateTime).Value = toDateTimePicker.Value;//@TODATE
SqlDataAdapter SDA = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
SDA.Fill(dt);
OrderDataGridView.DataSource = dt;
OrderDataGridView.Refresh();
OrderDataGridView.Enabled = true;
sqlConnection.Close();
}