【发布时间】:2013-07-05 17:58:04
【问题描述】:
我有一个 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;并测试了3次,每次下载几分钟(每次大约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 限制了查询执行时间。因此,一个可行的解决方案是使用计数器进行尝试/捕获,以跟踪在关闭连接之前检索了多少记录,并重新打开以计数器为起点的新连接,直到表大小用尽。
感谢大家的帮助!
【问题讨论】:
标签: c# mysql timeoutexception mysql.data