简体   繁体   中英

How get huge data from sql server using ado.net?

I'm getting error while fetching the data

Exception of type 'System.OutOfMemoryException' was thrown

Error getting line is dataAdapter.Fill(dataTable);

(2826000) records count in my table.

here is the code i'm using.

var dataTable = new DataTable();
var DicTableNameAndValues = new Dictionary<string, object>();

using (var connection = new SqlConnection(ConnectionString))
{
    connection.Open();
    var dataQuery = "SELECT * FROM  " + table;
    using (var command = new SqlCommand(dataQuery, connection))
    {
        var dataAdapter = new SqlDataAdapter(command);
        dataAdapter.Fill(dataTable);
        var result = dataTable.AsEnumerable().Skip(skip).Take(pageSize).ToList().Select(c => c.ItemArray);
        DicTableNameAndValues.Add(table, result);

    }
}

You can get a certain number of records as required by your application instead of all the records. You can create a stored procedure something to the effect of the following.

--Create a stored procedure with following parameters
DECLARE @skip int, @pagesize int

--Added testing values
SELECT @skip = 4, @pagesize = 3

--Give @tbl with your table name. 
/*If you already have an identity key then probably row_number function might not 
be required. But if the records are getting deleted as well then row_number is a
better option*/
SELECT * FROM
(SELECT ROW_NUMBER() over(ORDER BY Your_Col) AS ROWNUM, * FROM @tbl) as tbl
WHERE ROWNUM BETWEEN (@skip * @pagesize) + 1 and (@skip + 1) * (@pagesize)

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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