简体   繁体   English

C#/ ADO.NET-在数据表中使用SetField时会跳过行吗?

[英]C#/ADO.NET - Rows being skipped while using SetField in datatable?

I have a console application that is ran daily to update my database. 我有一个控制台应用程序,每天运行一次以更新我的数据库。

Everyday, the application pulls all the Ticket data into a datatable. 每天,应用程序都会将所有票证数据提取到数据表中。

In this particular case, I am trying to calculate whether each ticket in the database that has not been responded too, has passed the max response time allotted for that ticket (in hours). 在这种特殊情况下,我试图计算数据库中尚未响应的每个故障单是否都超过了为该故障单分配的最大响应时间(以小时为单位)。 Ticket reported date vs Today's date. 票务报告日期与今天的日期。

If the response time has passed a value of 0(false) is set in the Response_met column, else 1(true). 如果响应时间已过,则在Response_met列中将值设置为0(false),否则将设置为1(true)。

When new tickets are added to the database, their Response_Met is originally NULL until this update is ran and calculates whether or not the response_time has passed or not. 将新票证添加到数据库后,它们的Response_Met最初为NULL,直到运行此更新并计算response_time是否已过去。

This is the datatable before the udpate. 这是udpate之前的数据表。

在此处输入图片说明

The highlighted cell is currently NULL. 突出显示的单元格当前为NULL。 After the update, it should change to 0(false) because the Max_response_time is 1.00 HOUR, and the ticket was Reported in 2014, and does not have a response_time. 更新后,它应该更改为0(false),因为Max_response_time为1.00 HOUR,并且该故障单是在2014年报告的,并且没有response_time。

This is the datatable after the update, it hasent changed. 这是更新后的数据表,已更改。

在此处输入图片说明

Their are several instances of this happening in the datatable, I am just showing the first instance here. 它们是数据表中发生的几种情况,这里我只显示第一个情况。

Can anyone tell where the issue is? 谁能说出问题在哪里? Why are some rows being skipped and left NULL after the update? 为什么在更新后会跳过某些行并将其保留为NULL? It works for the majority of rows, except a handful of them. 它适用于大多数行,除了少数行。 Below is the update code I have written. 以下是我编写的更新代码。

using (SqlCommand cmd2 = new SqlCommand("SELECT INCIDENT_NO, Report_DT, MIN(theStart) as The_Start, RESPONSE_TIME, Response_met, max_response_time FROM Ticket_REPORT rr left JOIN TimeTracker ON Incident_NO=theTask where theStart is null and impact like '3%' or theStart is null and impact like '4%' GROUP BY INCIDENT_NO, Report_DT,RESPONSE_TIME,Response_met,MAX_RESPONSE_TIME", sqlConn))
            {
                var calc = new Calculation(holidays, new OpenHours("09:00;17:00"));//Calculates business hours
                SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
                dtResponse.Clear();
                da2.Fill(dtResponse);

                foreach (DataRow row in dtResponse.Rows)
                {
                    var reportDT = row.Field<string>(dtResponse.Columns[1]);
                     //Check if business hours have passed alloted response time
                    if (TimeSpan.FromMinutes(calc.getElapsedMinutes(DateTime.Parse(reportDT), DateTime.Now)).TotalHours > (double)row.Field<decimal>(dtResponse.Columns[5]))
                    {
                        row.SetField(dtResponse.Columns[4], 0);
                    }
                }
                da2.Dispose();
            }

您应该调用da2.Update(dtResponse)来实际更新数据库,否则您只是在更改数据行的行状态

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

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