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

时间:2013-07-08 11:57:29

标签: c# mysql timeoutexception mysql.data

我有一个MySQL数据库“DB”,其中有一个表“TABLE”的单个字段,它包含大约8百万行。我试图将该表下载到文本文件中,如下所示:

//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的起点直到表大小耗尽。 / p>

感谢您的帮助!

3 个答案:

答案 0 :(得分:2)

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

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

还有其他超时参数可能是问题,但我认为它就是那个。

如果您无权访问ini文件,请使用“限制”来获取有限的结果集。

首先,获取总行数(count(*)) 其次,感谢for

得到小的结果集
 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
 }

答案 1 :(得分:1)

将此添加到您的连接字符串:

default command timeout=240

并根据需要进行调整。

答案 2 :(得分:0)

你有几个选择

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

  2. 中设置CommandTimeout parameter
  3. 您可以使用会在服务器上生成文件的SELECT INTO

  4. 您可以使用也会在服务器上生成文件的mysqldump实用程序

  5. 修改 SO用户提到了其他类型的超时参数。我不是用户哪一个是关键