Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6900719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:34:55+00:00 2026-05-27T07:34:55+00:00

I am working on C# where I am reading a huge table from 1

  • 0

I am working on C# where I am reading a huge table from 1 database and loading it to my DataTable.

Since the table comprises of a huge set of rows (1,800,000+) and I keep getting an out-of-memory error I tried to break it down and copy it 100,000 rows at a time and clear up memory and redo till all the data in the table from the source gets loaded to my DataTable.

Can you just look at my code and tell me if I am on the right track? From what it looked to me I was reading the first 100,000 rows again and again and my program is running indefinitely.

Is there a counter I need to be adding to my DataTable? So that it adds the next set of rows???
My code snippet is below:

    public IoSqlReply GetResultSet(String directoryName, String userId, String password, String sql)
    {
        IoSqlReply ioSqlReply = new IoSqlReply();
        DataTable dtResultSet = new DataTable();
        IoMsSQL ioMsSQL = null;
        int chunkSize = 100000;
        try
        {
            using (OdbcConnection conn = new OdbcConnection(cs))
            {
                conn.Open();

                using (OdbcCommand cmd = new OdbcCommand(sql, conn))
                {
                    using (OdbcDataReader reader = cmd.ExecuteReader())
                    {


                        for (int col = 0; col < reader.FieldCount; col++)
                        {
                            String colName = reader.GetName(col);
                            String colDataType = reader.GetFieldType(col).ToString(); ;

                            dtResultSet.Columns.Add(reader.GetName(col), reader.GetFieldType(col));
                        }
                                                 // now copy each row/column to the datatable

                        while (reader.Read())       // loop round all rows in the source table
                        {
                            DataRow row = dtResultSet.NewRow();

                            for (int ixCol = 0; ixCol < reader.FieldCount; ixCol++)     // loop round all columns in each row
                            {
                                row[ixCol] = reader.GetValue(ixCol);
                            }


                            // -------------------------------------------------------------
                            // finished processing the row, add it to the datatable
                            // -------------------------------------------------------------

                            dtResultSet.Rows.Add(row);

                                GC.Collect();       // free up memory

                        }//closing while

                        ioSqlReply.DtResultSet = dtResultSet;       // return the data table
                        ioSqlReply.RowCount = dtResultSet.Rows.Count;
                        Console.WriteLine("DTRESULTSET:ROW COUNT FINAL : " + dtResultSet.Rows.Count);
                        ioSqlReply.Rc = 0;
                    }
                }
            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:34:56+00:00Added an answer on May 27, 2026 at 7:34 am

    You should limit your amount of rows in your Sql for example…

    SELECT TOP 10000 * FROM SomeTable;
    

    if you don’t do this, and you have 1.8M in your query, then there is no system able to handle it.

    But this will make your app to process only the first 10000 rows… if you need to process all rows, then you should iterate the execution of that sql unitl there are not more rows… for example

    public IoSqlReply GetResultSet(String directoryName, String userId, String password, String sql)
    {
        IoSqlReply ioSqlReply = new IoSqlReply();
        DataTable dtResultSet = new DataTable();
        IoMsSQL ioMsSQL = null;
        bool keepProcessing = true;
    
        try
        {
            using (OdbcConnection conn = new OdbcConnection(cs))
            {
                conn.Open();
    
                while (keepProcessing)
                {
                   using (OdbcCommand cmd = new OdbcCommand(sql, conn))
                   {
                       using (OdbcDataReader reader = cmd.ExecuteReader())
                       {
    
                          if (reader.HasRows)
                          {
                            for (int col = 0; col < reader.FieldCount; col++)
                            {
                               String colName = reader.GetName(col);
                               String colDataType = reader.GetFieldType(col).ToString(); ;
    
                               dtResultSet.Columns.Add(reader.GetName(col),     reader.GetFieldType(col));
                            }
                            // now copy each row/column to the datatable
    
                            while (reader.Read())       // loop round all rows in the source table
                            {
                                DataRow row = dtResultSet.NewRow();
    
                                for (int ixCol = 0; ixCol < reader.FieldCount; ixCol++)     // loop round all columns in each row
                                {
                                    row[ixCol] = reader.GetValue(ixCol);
                                }
    
    
                               // -------------------------------------------------------------
                               // finished processing the row, add it to the datatable
                               // -------------------------------------------------------------
    
                                dtResultSet.Rows.Add(row);
    
                                GC.Collect();       // free up memory
    
                            }//closing while
    
                            ioSqlReply.DtResultSet = dtResultSet;       // return the data table
                            ioSqlReply.RowCount = dtResultSet.Rows.Count;
                            Console.WriteLine("DTRESULTSET:ROW COUNT FINAL : " + dtResultSet.Rows.Count);
                            ioSqlReply.Rc = 0;
                          }
                          else
                          {
                            keepProcessing = false;
                          }
                       }
                    }
                }
            }
        }
    

    This is a very rough example… It can be improved but I think it is an easy fix for your problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

reading excel files from C# working well in 32 bit version server. It is
I am working on task involving reading from the socket trading quotes and I
After some reading from this tutorial , I have the following partly working code:
I was working on reading mails from gmail using webdriver and in between I
Writing and reading from LocalStorage is working fine from my popup and tab. However,
I'm currently working on a project trying to update a page reading from an
I'm working my way through some ASP.NET MVC reading and I have a web
I remember reading an article that an average programmer spends 90% of the working
I'm reading the Llama ( Learning Perl ) book, and working on the exercises.
I'm currently working on a PowerShell script to analyse VPN traffic by reading the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.