gsmaker
gsmaker

Reputation: 593

Exception from MySqlDataReader.Read() while there are huge number of records in C# Project

Currently I am working on a C# project. The purpose of the project is to read MySQL database records by executing select query. To read the database records, I am using MySqlDataReader class and perform ExecuteReader() function.

Database connection and reading sample code.

using (MySqlConnection conn = new MySqlConnection(dbConnectionString))
{
    conn.Open();
    using (MySqlCommand command = new MySqlCommand(query, conn))
    {
        using (MySqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
            // process data here
            }
        }
    }
}

But getting below exception from reader.Read() function if there are huge number of records (i.e. 20000 or more).

MySql.Data.MySqlClient.MySqlException
  HResult=0x80004005
  Message=Fatal error encountered during data read.
  Source=MySql.Data
  StackTrace:
   at MySql.Data.MySqlClient.MySqlDataReader.Read()

Inner Exception 1:
MySqlException: Reading from the stream has failed.

If there is anyone face the same, please share the solution. It will be highly appreciated.

Note: If there is same question available, please share the link.

Upvotes: 0

Views: 610

Answers (2)

Bradley Grainger
Bradley Grainger

Reputation: 28162

You are most likely experiencing a timeout issue. If you know this query may take a long time to run, you can increase the timeout on this one MySqlCommand:

using (MySqlCommand command = new MySqlCommand(query, conn))
{
    command.CommandTimeout = 600; // in seconds, i.e., ten minutes
    ....

Upvotes: 1

shaunteezie
shaunteezie

Reputation: 89

Try changing the net timeout settings.

MySqlCommand cmd = new MySqlCommand("set net_write_timeout=99999; set net_read_timeout=99999", con); cmd.ExecuteNonQuery();

Upvotes: 0

Related Questions