2016-08-03 30 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; –