将数据加载到数据表时,内存超出异常

时间:2018-06-04 13:59:33

标签: c# winforms performance datatable

我正在尝试从存储过程中获取数据,并尝试使用Load方法将这些数据加载到数据表中。以下是我的代码

     using (SqlConnection sqlConnection = new SqlConnection(connectionString))
        {
            DataTable addDataTable;
            using (SqlCommand orgCommand = new SqlCommand("dbo.GetTable", sqlConnection))
            {
                orgCommand.CommandType = CommandType.StoredProcedure;

                orgCommand.CommandTimeout = 0;
                sqlConnection.Open();
                using (SqlDataReader reader = orgCommand.ExecuteReader())
                {
                    addDataTable = new DataTable();
                    addDataTable.TableName = "TableName";
                    addDataTable.Load(reader);
                    dataSet.Tables.Add(addDataTable);
                }
            }
            dataGridView1.DataSource = addDataTable;

此存储过程会返回大量行,并在尝试加载数据表时,代码“addDataTable.Load(reader)'抛出以下内存超出异常。是否有任何其他替代方法或任何优化技术可将大量数据加载到数据表中?

System.OutOfMemoryException was unhandled
HResult=-2147024882
Message=Exception of type 'System.OutOfMemoryException' was thrown.
StackTrace:
   at System.Data.RBTree`1.TreePage..ctor(Int32 size)
   at System.Data.RBTree`1.AllocPage(Int32 size)
   at System.Data.RBTree`1.GetNewNode(K key)
   at System.Data.Index.InitRecords(IFilter filter)
   at System.Data.Index..ctor(DataTable table, IndexField[] indexFields, Comparison`1 comparison, DataViewRowState recordStates, IFilter rowFilter)
   at System.Data.DataTable.GetIndex(IndexField[] indexDesc, DataViewRowState recordStates, IFilter rowFilter)
   at System.Data.DataColumn.get_SortIndex()
   at System.Data.DataColumn.IsNotAllowDBNullViolated()
   at System.Data.DataTable.EnableConstraints()
   at System.Data.DataTable.set_EnforceConstraints(Boolean value)
   at System.Data.DataTable.EndLoadData()
   at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
   at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
   at System.Data.Common.LoadAdapter.FillFromReader(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
   at System.Data.DataTable.Load(IDataReader reader, LoadOption loadOption, FillErrorEventHandler errorHandler)
   at System.Data.DataTable.Load(IDataReader reader)
   at WindowsFormsApplication4.Form1..ctor() in C:\Users\Anish George\Documents\Visual Studio 2015\Projects\WindowsFormsApplication4\WindowsFormsApplication4\Form1.cs:line 35
   at WindowsFormsApplication4.Program.Main() in C:\Users\Anish George\Documents\Visual Studio 2015\Projects\WindowsFormsApplication4\WindowsFormsApplication4\Program.cs:line 19
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

提前谢谢。

此致 阿尼什

1 个答案:

答案 0 :(得分:0)

正如@SlapY和@Barry O'Kane已经指出的那样,你应该实现某种寻呼和/或延迟加载新数据,因为用户需要越来越多。 处理项目列表时,不要尝试一次加载所有内容,而是加载X项的组,其中X是对您的用例有意义的计数。

使用项目列表时,请记住,在大多数情况下,这些列表可能会很大。 潜在的意思是,大多数时候,你试图代表的事物清单没有硬性限制......(即用户数量,登录次数等)

例如,如果您为应用的用户创建数据表/数据网格,则必须按页面加载用户,一次加载X(可能是10,可能是50,用户可以在某个范围之间进行决定)页面大小)。 如果您没有页面,而是在用户向下滚动时想要延迟加载,那么您将再次必须在X项的行中引入新数据,以保持性能和可伸缩性