我编写了一个windows服务,它使用ssh(secureshell)将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.
当我随机更改线程保持时间的值时,上述两个错误会相互变化。
相关代码块:
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();
}
}
}
}
}
有什么建议可以解决这个异常吗?
暂无答案!
目前还没有任何答案,快来回答吧!