从MySQL数据库下载一个大的数据表到C#中的文本文件

本文关键字:数据表 文件 文本 一个 下载 数据库 MySQL | 更新日期: 2023-09-27 18:21:41

我有一个MySQL数据库"DB",其中有一个由单个字段组成的表"table",它包含大约800万行。我正试图将该表下载到一个文本文件中,如下所示:

//Open connection.
connection.Open();
//Create command.
MySqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT * FROM `DB`.`TABLE`";
//Execute command.
MySqlDataReader result = command.ExecuteReader();
//If result isn't null
if (result != null)
{
    //Open stream to write result.
    System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:'Users'StackOverflow'Desktop'ID_List.txt", true);
    //For each line row of result.
    while (result.Read())
    {
        //Write result in a line.
        file.WriteLine(result.GetValue(0).ToString());
    }
}
//Close connection.
connection.Close();

运行后,它开始下载数据并将其输出到文本文件,但仅一两分钟后,它就会给出:

An unhandled exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in test program.exe
Additional information: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.

为了让它发挥作用,我可以改变什么或做什么不同的事情?感谢您的任何建议和答案:)

[编辑]:我添加了command.CommandTimeout = 240;并测试了三次,每次下载几分钟(每次大约40MB的数据),然后给出:

An unhandled exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in test program.exe
Additional information: Fatal error encountered during data read.

以下是调试输出的最后几行:

at MySql.Data.MySqlClient.NativeDriver.FetchDataRow(Int32 statementId, Int32 columns) in :line 0
   at MySql.Data.MySqlClient.Driver.FetchDataRow(Int32 statementId, Int32 columns) in :line 0
   at MySql.Data.MySqlClient.ResultSet.GetNextRow() in :line 0
   at MySql.Data.MySqlClient.ResultSet.NextRow(CommandBehavior behavior) in :line 0
   at MySql.Data.MySqlClient.MySqlDataReader.Read() in :line 0</ExceptionString><InnerException><ExceptionType>System.IO.EndOfStreamException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Attempted to read past the end of the stream.</Message><StackTrace>   at MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[] buffer, Int32 offset, Int32 count) in :line 0
   at MySql.Data.MySqlClient.MySqlStream.LoadPacket() in :line 0</StackTrace><ExceptionString>System.IO.EndOfStreamException: Attempted to read past the end of the stream.
   at MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[] buffer, Int32 offset, Int32 count) in :line 0
   at MySql.Data.MySqlClient.MySqlStream.LoadPacket() in :line 0</ExceptionString></InnerException></InnerException></Exception></TraceRecord>
An unhandled exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in test program.exe
Additional information: Fatal error encountered during data read.
The program '[4548] test program.exe: Program Trace' has exited with code 0 (0x0).
The program '[4548] test program.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).

[EDIT2]:我添加MySqlDataReader result = command.ExecuteReader(System.Data.CommandBehavior.SingleResult);是为了纠正上述错误,现在我得到了:

An unhandled exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in test program.exe
Additional information: Query execution was interrupted

经过一些轻微的谷歌搜索,我认为是GoDaddy限制了查询执行时间。因此,一个可行的解决方案是使用带有计数器的try/catch来跟踪在连接关闭之前检索到的记录数量,并以计数器作为LIMIT的起点重新打开新连接,直到表大小用完为止。

谢谢你的帮助!

从MySQL数据库下载一个大的数据表到C#中的文本文件

增加my.ini文件中的wait_timeout参数。

http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_wait_timeout

还有其他超时参数可能是问题所在,但我认为这就是问题所在。

如果您没有访问ini文件的权限,请使用"限制"来获得有限的结果集。

首先,获取总行数(count(*))第二,由于的原因,获得较小的结果集

 for (int i=0; i<=numberOfRowsInYourTable ; i=i+1000){
      command.CommandText = "SELECT * FROM `DB`.`TABLE` LIMIT " + i + " " + new String(i + 1000);
      // Do what you want
 }

将其添加到连接字符串中:

default command timeout=240

并根据需要进行调整。

您有几个选项

  1. 您可以设置更长的超时这意味着您需要在连接字符串中设置CommandTimeout参数集

  2. 您可以使用SELECT INTO在服务器上生成文件

  3. 您可以使用mysqldump实用程序,它也可以在服务器上生成文件

编辑其他SO用户提到了其他类型的超时参数。我不是用户,哪一个是这里的关键