简体   繁体   English

C#windows窗体在datagridview中显示0,其中按小时分组的数据,但SQL开发人员显示正确的结果

[英]C# windows form shows 0 in datagridview where for data grouped by hour, but SQL developer shows the correct results

I have a query in Oracle to display number of transactions by hour, then the last column displays the sum for all hours. 我在Oracle中有一个查询以按小时显示事务数,然后最后一列显示所有小时的总和。 The query works fine in SQL developer. 该查询在SQL开发人员中工作正常。 However, when I try to display the results in a datagridview of my windows form application, I get 0 for all the hour data. 但是,当我尝试在我的Windows窗体应用程序的datagridview中显示结果时,我得到0表示所有小时数据。 I do however get the final total. 然而,我确实得到了最终的总数。

This is the first time I have had trouble using the date picker and displaying data in the datagridview. 这是我第一次使用日期选择器并在datagridview中显示数据时遇到问题。

Here is a screenshot of the results in SQL developer (I realize the total column does not equal the total of the columns shown. The screen shot does not show all columns.) 下面是SQL开发人员的结果截图(我意识到总列不等于显示的列总数。屏幕截图不显示所有列。)

在此输入图像描述

And here is the screenshot of the same data in window forms. 这是窗口形式中相同数据的屏幕截图。

在此输入图像描述

This is what I believe is the relevant SQL code. 这就是我认为的相关SQL代码。 Please forgive me if it is too much. 如果太多,请原谅我。

SELECT
 user_name,
 SUM(CASE
     WHEN substr(hr,11) = '19:00:00' THEN t
     ELSE 0
 END) AS eight_pm,
 SUM(CASE
     WHEN substr(hr,11) = '20:00:00' THEN t
     ELSE 0
 END) AS nine_pm,
 SUM(CASE
     WHEN substr(hr,11) = '21:00:00' THEN t
     ELSE 0
 END) AS ten_pm,
 SUM(CASE
     WHEN substr(hr,11) = '22:00:00' THEN t
     ELSE 0
 END) AS eleven_pm,
 SUM(t) total
 FROM
 (
     SELECT
         user_name,
         COUNT(*) t,
         TO_CHAR(trunc(last_update_date,'HH24') ) AS hr
     FROM
         cte
     GROUP BY
         user_name,
         TO_CHAR(trunc(last_update_date,'HH24') )
 )
 GROUP BY
 user_name
 ORDER BY
 user_name

I am not doing anything with the date picker other that using it as a parameter to query the data. 我没有对日期选择器做任何事情,使用它作为查询数据的参数。 I can show the custom format or some of the C# code if necessary. 如有必要,我可以显示自定义格式或一些C#代码。

Please note, I will not be able to test any solutions until tomorrow morning. 请注意,我明天早上才能测试任何解决方案。 Thank you for your time. 感谢您的时间。

Update Adding code for populating datagridview: 更新添加用于填充datagridview的代码:

 private void btnSecondShiftStats_Click(object sender, EventArgs e)
    {
        var start = startDatePicker.Value;
        var end = endDatePicker.Value;
        string name = tbName.Text;

        dvgSecondShiftStats.DataSource = _secondShiftStatsData.GetStats(start, end, name);
    }

The GetStats method: GetStats方法:

   public List<SecondShiftStats> GetStats(DateTime fromDate, DateTime toDate, string uName)
    {
        var statList = new List<SecondShiftStats>();
        var conString = ConfigurationManager.ConnectionStrings["SecondShiftStatsConnection"].ConnectionString;
        using (OracleConnection con = new OracleConnection(conString))
        {
            string sql = @"WITH loaded AS (
                                             SELECT...";

            using (OracleCommand cmd = new OracleCommand(sql, con))
            {
                cmd.Parameters.Add(new OracleParameter(":start_time", OracleDbType.Date)).Value = fromDate;
                cmd.Parameters.Add(new OracleParameter(":end_time", OracleDbType.Date)).Value = toDate;

                cmd.Parameters.Add("name", uName);

                con.Open();
                using (OracleDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var stats = new SecondShiftStats();
                        stats.USER_NAME = reader["USER_NAME"].ToString();
                        stats.THREE_PM = Convert.ToInt32(reader["THREE_PM"]);
                        stats.FOUR_PM = Convert.ToInt32(reader["FOUR_PM"]);
                        stats.FIVE_PM = Convert.ToInt32(reader["FIVE_PM"]);
                        stats.SIX_PM = Convert.ToInt32(reader["SIX_PM"]);
                        stats.SEVEN_PM = Convert.ToInt32(reader["SEVEN_PM"]);
                        stats.EIGHT_PM = Convert.ToInt32(reader["EIGHT_PM"]);
                        stats.NINE_PM = Convert.ToInt32(reader["NINE_PM"]);
                        stats.TEN_PM = Convert.ToInt32(reader["TEN_PM"]);
                        stats.ELEVEN_PM = Convert.ToInt32(reader["ELEVEN_PM"]);
                        stats.TOTAL = Convert.ToInt32(reader["TOTAL"]);

                        statList.Add(stats);
                    }
                }

            } 

        }
        return statList;
    }

I figured out solution to this problem. 我想出了解决这个问题的方法。 I have another app showing the same query, but without the grouping. 我有另一个应用程序显示相同的查询,但没有分组。 I saw a difference in the datetime format in sql developer vs the datagridview. 我在sql developer和datagridview中看到了datetime格式的不同。

SQL Developer: SQL开发人员:

在此输入图像描述

Datagridview: datagridview的:

在此输入图像描述

And finally the format of the Datetimepicker: 最后是Datetimepicker的格式:

在此输入图像描述

I never changed the way the datgridview displays the date. 我从未改变datgridview显示日期的方式。 I only customized the datetimepicker and used it as a parameter in the query. 我只定制了datetimepicker并将其用作查询中的参数。 This appeared to be the issue. 这似乎是个问题。

So I changed the relevant column in the select statement to: 所以我将select语句中的相关列更改为:

TO_CHAR(last_update_date, 'DD/MON/YYYY HH24:MI:SS')AS last_update_date.

Of course, I knew I would have to change my final select to: 当然,我知道我必须将我的最终选择改为:

select ...
case when substr(hr,11)...

to

select ...
case when substr(hr,13)...

Or so I thought. 或者我想。 I would get an ORA-01722: invalid-number message when I tried to run the query in SQL Developer. 当我尝试在SQL Developer中运行查询时,我会得到一个ORA-01722:invalid-number消息。

I determined it was in the grouping statement again, and looked for other solutions to group by the hour. 我确定它再次出现在分组声明中,并寻找按小时分组的其他解决方案。 I tried using the accepted answer from this Stack Overflow question , but received a message stating the hour number must be between 1 and 12, even though I have my format set to 'HH24'. 我尝试使用此Stack Overflow问题中接受的答案,但收到一条消息,说明小时编号必须在1到12之间,即使我的格式设置为'HH24'。

To fix this issue, I changed the final subquery to: 要解决此问题,我将最终子查询更改为:

SELECT
 user_name,
 SUM(CASE
     WHEN hr = '19' THEN t
     ELSE 0
 END) AS eight_pm,
 SUM(CASE
     WHEN hr  = '20' THEN t
     ELSE 0
 END) AS nine_pm,
 SUM(CASE
     WHEN hr = '21' THEN t
     ELSE 0
 END) AS ten_pm,
 SUM(CASE
     WHEN hr = '22' THEN t
     ELSE 0
 END) AS eleven_pm,
 SUM(t) total
 FROM
 (
     SELECT
         user_name,
         COUNT(*) t,
         substr(last_update_date,13,2)as hr
     FROM
         cte
     GROUP BY
         user_name,
        substr(last_update_date,13,2)
 )
 group by
 user_name
 order by
 user_name

After making these changes, my applications displays the results I expect it to: 进行这些更改后,我的应用程序会显示我期望的结果:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM