c# – MySqlClient EndOfStreamException:尝试读取流结束时发生致命错误
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – MySqlClient EndOfStreamException:尝试读取流结束时发生致命错误,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4280字,纯文字阅读大概需要7分钟。
内容图文
![c# – MySqlClient EndOfStreamException:尝试读取流结束时发生致命错误](/upload/InfoBanner/zyjiaocheng/889/2bac62750b1d4ea09b44b304db38da97.jpg)
我开发了一个Windows服务来从csv读取数据并将它们写回数据库.
执行程序后,它将正常工作,直到几分钟后到达下面的行时触发错误
cmd.ExecuteNonQuery();
错误的屏幕截图:
数据已写入数据库,直到弹出此错误.
触发错误最多需要2-3分钟.
我将包含此问题的相关代码块.
public void Insert()
{
if (this.OpenConnection() == true)
{
using(var reader = new StreamReader(@"C:\Users\Admin\source\Bargstedt.csv"))
{
List<string> listA = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
string querynew = "INSERT INTO new_jobs"
+ "(job_reference,status,code,no1,no2,no3,dimension,date_assigned,root,variable,number,stable,constant)"
+ "VALUES (?jobNo, ?strClientName, ?strClientReference, ?strJobCategory, ?datCommisioned, ?datPromisedDelivery, ?division, ?date_assigned, ?root, ?variable, ?number, ?stable, ?constant)";
MySqlCommand cmd = connection.CreateCommand();
cmd.CommandText= querynew;
cmd.Parameters.Add("?jobNo", MySqlDbType.VarChar).Value = (values[0]);
cmd.Parameters.Add("?strClientName", MySqlDbType.VarChar).Value =(values[1]);
cmd.Parameters.Add("?strClientReference", MySqlDbType.VarChar).Value = values[2];
cmd.Parameters.Add("?strJobCategory", MySqlDbType.VarChar).Value = values[3];
cmd.Parameters.Add("?datCommisioned", MySqlDbType.VarChar).Value = values[4];
cmd.Parameters.Add("?datPromisedDelivery", MySqlDbType.VarChar).Value = values[5];
cmd.Parameters.Add("?division", MySqlDbType.VarChar).Value = values[7];
cmd.Parameters.Add("?date_assigned", MySqlDbType.VarChar).Value = values[9];
cmd.Parameters.Add("?root", MySqlDbType.VarChar).Value = values[10];
cmd.Parameters.Add("?variable", MySqlDbType.VarChar).Value = values[11];
cmd.Parameters.Add("?number", MySqlDbType.VarChar).Value = values[12];
cmd.Parameters.Add("?stable", MySqlDbType.VarChar).Value = values[13];
cmd.Parameters.Add("?constant", MySqlDbType.VarChar).Value = values[14];
cmd.ExecuteNonQuery(); **error line
}
}
this.CloseConnection();
}
}
为了更清楚,我还将包含csv文件和数据库结构的屏幕截图.
在csv的行中间有一些空格,这就是我在源代码中跳过第6行和第8行的原因.
phpMyAdmin数据库表的屏幕截图
编辑:
MySql.Data.MySqlClient.MySqlException
HResult=0x80004005
Message=Fatal error encountered during command execution.
Source=MySql.Data
StackTrace:
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
at SSHtest.DBConnect.Insert() in C:\Users\Admin\source\repos\backup new\SSHtest\SSHtest\Program.cs:line 248
at SSHtest.Service1.timer1_Tick(Object sender, ElapsedEventArgs e) in C:\Users\Admin\source\repos\backup new\SSHtest\SSHtest\Service1.cs:line 87
at System.Timers.Timer.MyTimerCallback(Object state)
Inner Exception 1:
MySqlException: Fatal error encountered attempting to read the resultset.
Inner Exception 2:
MySqlException: Reading from the stream has failed.
Inner Exception 3:
EndOfStreamException: Attempted to read past the end of the stream.
解决方法:
这是MySQL Connector / NET中的very frequently reported错误.
我见过社区发现的最佳解决方案是posted by Rui Fan:
I’ve identified the root cause of one case of this exception. If you always see this exception at the first connection to the database, my solutions may apply to you. The 3 possible solutions:
Solution 1: If SSL is not required. Since it is caused by SSL, we can turn off SSL by appending “SslMode=None” to the connection string.
Solution 2: If SSL is required, server identity is important and needs to be verified. Connect the server to the internet and try again.
Solution 3: If SSL is required but the server identity is not important. Typically SSL is only used to encrypt the network transport in this case. We can turn off CTL update:
- Press
Win+R
to open the “Run” dialog- Type
gpedit.msc
and press Enter- In the “Local Group Policy Editor”, expand “Computer Configuration”, expand “Administrative Templates”, expand “System”, expand “Internet Communication Management”, and then click “Internet Communication settings”.
- In the details panel, double-click “Turn off Automatic Root Certificates Update”, clickEnabled, then click OK. This change will be effective immediatelly without restart.
More details can be found in 07002.
或者,您可以将MySQL ADO.NET库切换到MySqlConnector.它fixes many MySQL连接器/ NET错误,并且从未报告此特定问题.
内容总结
以上是互联网集市为您收集整理的c# – MySqlClient EndOfStreamException:尝试读取流结束时发生致命错误全部内容,希望文章能够帮你解决c# – MySqlClient EndOfStreamException:尝试读取流结束时发生致命错误所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。