我编写了一个Windows服务,该服务使用SSH(安全外壳)将数据从CSV文件复制到PhpMyAdmin在线数据库。
连接打开时出现错误触发,
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)
作为解决方案,我决定保留当前线程并在连接打开之前添加以下行。
Task.Delay(TimeSpan.FromMilliseconds(10000)).Wait();
不幸的是,这导致了另一个错误,
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.
当我随机更改线程保持时间的值时,上述2个错误会彼此不同。
相关代码块:
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();
}
}
}
}
}
关于如何解决此异常的任何建议?