我有一个偶尔会执行的WPF应用程序(每隔几天)。它连接到MySQL数据库并在那里转储一些信息。这是我用来连接的代码:
// connection to the database server
public MySqlConnection con;
public Window1()
{
InitializeComponent();
//initialize connection variables
con = new MySqlConnection();
con.ConnectionString = @"Server = xxx; Database = xxx; Uid = xxx ; Pwd = 'xxx'; charset=utf8;";
}
public bool OpenConnection()
{
try
{
con.Open();
return true;
}
catch (MySqlException ex)
{
//logging any error messages here to custom log files
return false;
}
}
该应用程序工作好几个月。我没有触及它,除了偶尔运行它之外,它会成功连接到服务器,执行它的工作然后退出,在此过程中关闭连接。
前几天我试图再次运行它,并收到以下错误消息:
MySql.Data.MySqlClient.MySqlException: Reading from the stream has failed. ---> System.IO.IOException: Unable to read data from the transport connection: A non-blocking socket operation could not be completed immediately. ---> System.Net.Sockets.SocketException: A non-blocking socket operation could not be completed immediately
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at MySql.Data.Common.MyNetworkStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at MySql.Data.MySqlClient.TimedStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.BufferedStream.Read(Byte[] array, Int32 offset, Int32 count)
at MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[] buffer, Int32 offset, Int32 count)
at MySql.Data.MySqlClient.MySqlStream.LoadPacket()
--- End of inner exception stack trace ---
at MySql.Data.MySqlClient.MySqlStream.LoadPacket()
at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
at MySql.Data.MySqlClient.NativeDriver.Open()
at MySql.Data.MySqlClient.Driver.Open()
at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
at MySql.Data.MySqlClient.MySqlPool.GetConnection()
at MySql.Data.MySqlClient.MySqlConnection.Open()
我可以使用phpMyAdmin使用连接字符串中的相同参数成功连接到数据库,我还有一个小型在线Web PHP应用程序,它从同一个数据库中读取数据,并且还成功连接并运行查询。但是,WPF应用程序不再是这种情况,所以任何建议可能是什么问题以及出了什么问题?
P.S。 PHP应用程序使用得更少,因此我认为这不是一个高需求问题,因为除了WPF应用程序之外,没有人实际访问此数据库。此外,我已尝试连续运行3天(白天不同时间),每次都出现同样的错误。