zoukankan      html  css  js  c++  java
  • 将一个DataTable分解成多个DataTable

    我们所接触的一个系统在导出数据到Excel的时候,产生了内存溢出的错误。原因在于数据过大,它导出是将所有数据存放在一个DataSet的一个表中,再将这个数据集放入session,在导出功能所在的页面再读取该session的值,并绑定在一个DataGrid,再进行相关导出处理。因为系统不是我们开发的,我们就打算在数据存入session的时候,将数据表分解成多个表存入DataSet,这样在绑定DataGrid并处理的时候,能够一个个table的处理。测试后证明,方法是成功了。下面就把代码一贴,有类似需求的可以大概看看,希望能有所帮助。

    C# 代码:

     /// <summary>
            
    /// 分解数据表
            
    /// </summary>
            
    /// <param name="originalTab">需要分解的表</param>
            
    /// <param name="rowsNum">每个表包含的数据量</param>
            
    /// <returns></returns>
            public DataSet SplitDataTable(DataTable originalTab, int rowsNum)
            {            
                
    //获取所需创建的表数量
                int tableNum = originalTab.Rows.Count / rowsNum;

                
    //获取数据余数
                int remainder = originalTab.Rows.Count % rowsNum;
                
                DataSet ds 
    = new DataSet();

                
    //如果只需要创建1个表,直接将原始表存入DataSet
                if (tableNum == 0)
                {
                    ds.Tables.Add(originalTab);
                }
                
    else
                {
                    DataTable[] tableSlice 
    = new DataTable[tableNum];
                
                    
    //Save orginal columns into new table.            
                    for (int c = 0; c<tableNum; c++)
                    {
                        tableSlice[c] 
    = new DataTable();
                        
    foreach(DataColumn dc in originalTab.Columns)
                        {                    
                            tableSlice[c].Columns.Add(dc.ColumnName,dc.DataType);                    
                        }
                    }                                                            
                    
    //Import Rows
                    for (int i = 0; i < tableNum; i ++)
                    {
                        
    // if the current table is not the last one
                        if (i != tableNum -1)
                        {
                            
    for(int j = i*rowsNum ; j < ((i+1)*rowsNum); j++)
                            {
                                tableSlice[i].ImportRow(originalTab.Rows[j]);
                            }
                        }
                        
    else
                        {
                            
    for(int k = i*rowsNum ; k < ((i+1)*rowsNum+remainder); k++)
                            {
                                tableSlice[i].ImportRow(originalTab.Rows[k]);
                            }
                        }
                    }            
                
                    
    //add all tables into a dataset                
                    foreach(DataTable dt in tableSlice)
                    {
                        ds.Tables.Add(dt);
                    }
                }
                
    return ds;
            }


    VB.NET 代码:
      '===============================================================================
        ' Author: Ray Chang
        ' Date: 2007/04/11
        ' Description: This function splits a givin datatabe into several tables and 
        '              create a new dataset to hold these tables. 
        '================================================================================
        Public Function SplitDataTable(ByVal originalTab As DataTable, ByVal rowsNum As IntegerAs DataSet

            
    Dim tableNum As Integer = originalTab.Rows.Count \ rowsNum
            
    Dim remainder As Integer = originalTab.Rows.Count Mod rowsNum
            
    Dim ds As DataSet = New DataSet
            
    'if one table is big enough to store, use one table
            If tableNum = 0 Then
                ds.Tables.Add(originalTab)
            
    Else

                
    Dim tableSlice(tableNum - 1As DataTable

                
    'Save orginal columns into new table
                Dim c As Integer
                
    For c = 0 To (tableNum - 1)
                    tableSlice(c) 
    = New DataTable
                    
    For Each dc As DataColumn In originalTab.Columns
                        tableSlice(c).Columns.Add(dc.ColumnName, dc.DataType)
                    
    Next
                
    Next

                
    'Import Rows
                Dim i As Integer
                
    For i = 0 To (tableNum - 1)
                    
    'if the current table is not the last table
                    If i <> tableNum - 1 Then
                        
    Dim j As Integer
                        
    For j = i * rowsNum To (((i + 1* rowsNum) - 1)
                            tableSlice(i).ImportRow(originalTab.Rows(j))
                        
    Next
                    
    Else
                        
    Dim k As Integer
                        
    For k = i * rowsNum To (((i + 1* rowsNum + remainder) - 1)
                            tableSlice(i).ImportRow(originalTab.Rows(k))
                        
    Next
                    
    End If
                
    Next

                
    'Add all tables into a dataset        
                For Each dt As DataTable In tableSlice
                    ds.Tables.Add(dt)
                
    Next
            
    End If

            
    'return dataset
            SplitDataTable = ds
        
    End Function

    先用C#写的,可是系统用的却是VB.NET,只能又转成了VB.NET. 整个方法返回一个包含了分解后的table的DataSet, 所传入的参数主要2个:一是所需要分解的表,还有一个是每一个表所包含的数据量,而最后一个表将会包含设定的数据量加上余数。至于转换后如何操作,我就不罗嗦了。

    这个方法应用的地方不多,除非特别案例,如果那个系统从开头设计好,估计后期也不用做这种改动。总之贴出来,能用到大家就用吧。
    - Ray Chang  2007年4月11日
    转自:http://www.cnblogs.com/Rayinuk/archive/2008/11/04/709336.html
  • 相关阅读:
    迭代器
    逻辑量词
    How to distinguish between strings in heap or literals?
    Is the “*apply” family really not vectorized?
    power of the test
    The Most Simple Introduction to Hypothesis Testing
    析构函数允许重载吗?
    C++中析构函数的作用
    在C#中的构造函数和解析函数
    c++构造函数与析构函数
  • 原文地址:https://www.cnblogs.com/lifuyun/p/lifuyun09091701.html
Copyright © 2011-2022 走看看