Expected to read 4 header bytes but only received

2019-02-20 04:42发布

I have written a windows service which uses SSH (Secure Shell) to copy data from a CSV file to PhpMyAdmin online database.

There was an error trigger when the connection opens,

MySql.Data.MySqlClient.MySqlProtocolException
  HResult=0x80131509
  Message=Packet received out-of-order. Expected 2; got 1.
  Source=mscorlib
  StackTrace:
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

As a solution, I decided to hold the current thread and add the following line before the connection opens.

Task.Delay(TimeSpan.FromMilliseconds(10000)).Wait();

Unfortunately, that leads to an another error,

MySql.Data.MySqlClient.MySqlException
  HResult=0x80004005
  Message=Failed to read the result set.
  Source=MySqlConnector
  StackTrace:
Inner Exception 1:
EndOfStreamException: Expected to read 4 header bytes but only received 0.

When I randomly changed the value of the time the thread holds, the above 2 errors varies with each other.

Related code chunk:

        Public  void insert(string connString){
        using (var conn = new MySqlConnection(connString))
        {

             Task.Delay(TimeSpan.FromMilliseconds(10000)).Wait();
                conn.Open();
            using(var reader = new StreamReader(@"C:\Users\Admin\source\Bargstedt.csv"))
            {
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    var values = line.Split(',');
                    string querynew = "INSERT INTO jobs"
                                + "(nJobNumber,strClientReference,datPromisedDelivery)" 
                                + "VALUES (@jobNo, @strClientName, @strClientReference)";

                    using (var cmd = new MySqlCommand())
                    {

                        cmd.Connection = conn;
                        cmd.CommandText= querynew;
                        cmd.Parameters.AddWithValue("jobNo", values[0]);
                        cmd.Parameters.AddWithValue("strClientName", values[1]);


                        cmd.Parameters.AddWithValue("strClientReference",values[2]);


                        cmd.ExecuteNonQuery();
                    }
                }
            }
        }
    }

Any suggestions how to fix this exception?

0条回答
登录 后发表回答