试图在MySQL中读取流错误的过去结束

时间:2011-06-08 15:26:08

标签: .net mysql ioexception connector

我遇到了MySQL的问题,我遇到以下错误。

MySqlClient.MySqlException: Fatal error encountered during command execution. ---> 
MySql.Data.MySqlClient.MySqlException: Fatal error encountered attempting to read the resultset. ---> 
MySql.Data.MySqlClient.MySqlException: Reading from the stream has failed. ---> 
System.IO.EndOfStreamException: Attempted to read past the end of the stream.

当我一夜之间运行时会发生此错误。并且它很少发生,所以很难找到为什么会发生这种情况。我使用.NET 3.5和MySQLConnector 6.2.4.0。

我正在使用以下代码运行它。

        public DataSet Read(String query, List<KeyValuePair<String, object>> parameters)
    {
        MySqlDataAdapter adapter = null;
        DataSet returnVal = null;
        if (query != null && query.Length > 0)
        {
            try
            {
                returnVal = new DataSet();
                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                }
                query = SQLHelper.formatSQL(query);
                MySqlCommand command = buildCommand(connection, query, parameters);

                Stopwatch stopwatch = new Stopwatch();

                command.CommandTimeout = 120;
                adapter = new MySqlDataAdapter(command);
                log.Debug(adapter.SelectCommand.CommandText);
                stopwatch.Start();
                adapter.Fill(returnVal);
                stopwatch.Stop();
                if (stopwatch.ElapsedMilliseconds < 150)
                {
                    log.Debug(stopwatch.ElapsedMilliseconds + "ms to run query");
                }
                else
                {
                    StringBuilder sb = new StringBuilder("");
                    if (parameters != null)
                    {
                        foreach (KeyValuePair<String, object> kvp in parameters)
                        {
                            sb.Append(kvp.Key);
                            sb.Append(" = ");
                            if (kvp.Value != null)
                            {
                                sb.Append(kvp.Value.ToString());
                            }
                            else
                            {
                                sb.Append("NULL");
                            }
                            sb.Append(", ");
                        }
                    }
                    log.Warn(stopwatch.ElapsedMilliseconds + "ms to run query: " + adapter.SelectCommand.CommandText + "Values: " + sb.ToString());
                }
            }
            catch (MySqlException msqlex)
            {
                log.Error(msqlex);
                returnVal = null;
                MessageBox.Show(query + "\r\n" + msqlex.ToString());
            }
            finally
            {
            }
        }
        else
        {
            log.Error("Query is empty. Returning null");
        }
        return returnVal;
    }

正如您所看到的,我并非手动尝试阅读任何内容&gt;&lt;我正在做一个adapter.fill(x),所以我无法控制阅读流的结尾。

为什么会发生这种情况?

如果您需要更多详细信息,请与我们联系。

1 个答案:

答案 0 :(得分:7)

当超过超时时,它看起来会抛出。有一个错误修复它没有这样做,所以如果他们修复它,现在它确实。

connector-net-en.a4.pdf (MySQL Documentation)

当超过net_write_timeout时,MySQL Connector / NET没有抛出EndOfStreamException异常。 (Bug#53439)