运行循环时出现内存异常

时间:2013-06-13 16:40:49

标签: c# .net

此时我很困惑我有以下代码:

            while (dal.Read())
            {

                if (FiltersAppliedForThisJob(dal.GetInt(0)))
                   continue;
                var j = 0;
                if (SettingsManager.OpsMgrSettings.JobViewServiceList.IsNotNull() &&
                    SettingsManager.OpsMgrSettings.JobViewServiceList.Count > 0)
                    if (!ThisJobHasASupportedService(dal.GetInt(0)))
                        continue;

                var newJobGridRow = AddRowToJobGridWithJobID(dal.GetInt(0),
                                                             ref j);
                BoldThisJobGridRowIfItIsUpdated(newJobGridRow, dal);
                var jobno = dal2.GetJobNo(dal.GetInt(0).ToString());
                if (locns2view.Length > 0)
                    _jobgrid.Rows[newJobGridRow].Visible = locns2view.Contains(jobno.Substring(0, 2));

                var col = 2;
                if (j < _jobgrid.ColumnCount)
                    for (i = 0; i < str1.Length; i++)
                    {
                        if (str1[i].Length <= 0)
                            continue;

                        if (!dal.IsFieldNull(col))
                            PlaceValuesInJobGridCells(dtype, i, dal2, str1, jobno, newJobGridRow, j, dal, col);
                        j++;
                        col++;
                    }
                if (_jobgrid[0, newJobGridRow].Value.ToString() == prevjobid)
                    _jobgrid.Rows[newJobGridRow].Selected = true;
            }

            SortJobGridColumns();
        }

似乎内存不足错误来自上面的代码。但是,如果我在调用FiltersAppliedForThisJob(dal.GetInt(0)之前放入MessageBox,则不会出现错误。例如下面的代码:

            while (dal.Read())
            {
                MessageBox.Show("Done"); // for debugging purposes

                if (FiltersAppliedForThisJob(dal.GetInt(0)))
                   continue;
                var j = 0;
                if (SettingsManager.OpsMgrSettings.JobViewServiceList.IsNotNull() &&
                    SettingsManager.OpsMgrSettings.JobViewServiceList.Count > 0)
                    if (!ThisJobHasASupportedService(dal.GetInt(0)))
                        continue;

                var newJobGridRow = AddRowToJobGridWithJobID(dal.GetInt(0),
                                                             ref j);
                BoldThisJobGridRowIfItIsUpdated(newJobGridRow, dal);
                var jobno = dal2.GetJobNo(dal.GetInt(0).ToString());
                if (locns2view.Length > 0)
                    _jobgrid.Rows[newJobGridRow].Visible = locns2view.Contains(jobno.Substring(0, 2));

                var col = 2;
                if (j < _jobgrid.ColumnCount)
                    for (i = 0; i < str1.Length; i++)
                    {
                        if (str1[i].Length <= 0)
                            continue;

                        if (!dal.IsFieldNull(col))
                            PlaceValuesInJobGridCells(dtype, i, dal2, str1, jobno, newJobGridRow, j, dal, col);
                        j++;
                        col++;
                    }
                if (_jobgrid[0, newJobGridRow].Value.ToString() == prevjobid)
                    _jobgrid.Rows[newJobGridRow].Selected = true;
            }

            SortJobGridColumns();
        }

另外,如果我删除了函数调用FiltersAppliedForThisJob(dal.GetInt(0),我就不会收到错误。

这是功能代码:

  private bool FiltersAppliedForThisJob(int p)
    {
        var filter_datefrom = SettingsManager.OpsMgrSettings.MainView_DateFrom;
        var filter_dateto = SettingsManager.OpsMgrSettings.MainView_DateTo;
        var filter_status = SettingsManager.OpsMgrSettings.MainView_JobStatus;
        var jobNumTbl = dal.Get_JobNumber(p);
        var jobStatus = GetJobStatus(p);
        var jobdate = jobNumTbl.Date;
        var jobdate_res = false;
        var jobstat_res = false;
        var isFiltered = false;

        if (jobdate.HasValue)
        {
            //both have values
            if (filter_datefrom.HasValue && filter_dateto.HasValue)
            {
                if (DateTime.Compare(jobdate.Value.Date, filter_datefrom.Value.Date) < 0 ||
                    DateTime.Compare(jobdate.Value.Date, filter_dateto.Value.Date) > 0)
                    jobdate_res = true;
            }
            //only datefrom has value
            else if (filter_datefrom.HasValue)
            {
                if (DateTime.Compare(jobdate.Value.Date, filter_datefrom.Value.Date) < 0)
                    jobdate_res = true;
            }
            //only datato has value
            else if (filter_dateto.HasValue)
            {
                if (DateTime.Compare(jobdate.Value.Date, filter_dateto.Value.Date) > 0)
                    jobdate_res = true;
            }
        }

        if (jobStatus.HasValue)
        {
            if (!filter_status.Equals(2))
                if (!filter_status.Equals(jobStatus))
                    jobstat_res = true;
        }

        if (jobdate_res || jobstat_res)
            isFiltered = true;


        return isFiltered;
    }

不确定这是否足够。如果您需要我更多,请告诉我。

1 个答案:

答案 0 :(得分:0)

问题解决了。应用程序在过滤之前从数据库中提取所有信息,这是很多数据。在加载数据之前,我在查询中添加了额外的过滤功能。现在一切都很好。谢谢大家