2016-08-03 26 views
1

我构建的应用程序备份我的MySQL数据库的服务器,一天一天,数据库变得比以前大了,这引起一些次错误(从我的角度来看):超时时间已在MySQL备份 - C#

消息:超时过期。操作完成之前超时的时间或服务器没有响应。 完整:MySql.Data.MySqlClient.MySqlException(0x80004005):超时过期。操作完成之前超时的时间或服务器没有响应。 ---> System.TimeoutException:在IO操作中超时 at MySql.Data.MySqlClient.TimedStream.StopTimer() 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() 在MySql.Data.MySqlClient.MySqlStream.ReadPacket() 在MySql.Data.MySqlClient.NativeDriver.FetchDataRow(的Int32 statementId,的Int32列) 在MySql.Data.MySqlClient.Driver.FetchDataRow (的Int32 statementId,的Int32列) 在MySql.Data.MySqlClient.ResultSet.GetNextRow() 在MySql.Data.MySqlClient.ResultSet.NextRow(的CommandBehavior行为) 在MySql.Data.MySqlClient.MySqlDataReader.Read() 在MySql.Data.MySqlClient.ExceptionInterceptor.Throw(例外的例外) 在MySql.Data.MySqlClient .MySqlConnection.Throw(异常前) 在MySql.Data.MySqlClient.MySqlConnection.HandleTimeoutOrThreadAbort(异常前) 在MySql.Data.MySqlClient.MySqlDataReader.Read() 在MySql.Data.MySqlClient.MySqlBackup.Export_RowsData(字符串表名,字符串selectSQL) 在MySql.Data.MySqlClient.MySqlBackup.Export_Rows(字符串表名,字符串selectSQL) 在MySql.Data.MySqlClient.MySqlBackup.Export_TableRows() 在MySql.Data.MySqlClient.MySqlBackup.ExportStart() 在MySql.Data.MySqlClient.MySqlBackup.ExportToFile(字符串文件路径) 在MYSQL_Auto_Backup.Form1.Backup()在C:\用户\贝拉尔\文件\的Visual Studio 2012 \项目\ MYSQL自动备份\ MYSQL自动备份\ Form1中。 CS:行132

代码:

// Backup... 
      DateTime Time = DateTime.Now; 
      year = Time.Year; 
      month = Time.Month; 
      day = Time.Day; 
      hour = Time.Hour; 
      minute = Time.Minute; 
      second = Time.Second; 
      millisecond = Time.Millisecond; 

      //Save file to Path with the current date as a filename 
      string path; 
      path = txb_Path.Text + year + "-" + month + "-" + day + "--" + hour + "-" + minute + "-" + second + ".sql"; 
      file = path; 
      using (MySqlConnection conn = new MySqlConnection(connectionString)) 
      { 
       using (MySqlCommand cmd = new MySqlCommand()) 
       { 
        using (MySqlBackup mb = new MySqlBackup(cmd)) 
        { 
         cmd.Connection = conn; 
         conn.Open(); 
         mb.ExportToFile(file); 
         conn.Close(); 
        } 
       } 
      } 

回答

1

您可以使用 “的CommandTimeout” 属性更改超时。

有很多的,你可能要考虑,以及执行备份操作(例如,从数据库的管理工具)的其他方式。例如,请参阅以下内容:
https://dev.mysql.com/doc/mysql-enterprise-backup/3.11/en/meb-scheduled-backups.html

另一个小问题。以下行:

string path; 
path = txb_Path.Text + year + "-" + month + "-" + day + "--" + hour + "-" + minute + "-" + second + ".sql"; 
file = path; 

为什么不这样做

file = String.Format("{0}{1}-{2}-3--{4}-{5}...", txb_Path.Text, year, month...); 

字符串连接像你这样做实际上是相当昂贵的,因为.NET .NET字符串是不可变的。

+0

我做到了通过添加:mb.Command.CommandTimeout = int.MaxValue; –