2013-02-18 138 views
3

我使用following code來備份MYSQL數據庫。使用MySqlBackup.NET導出數據庫時出現異常

private void button2_Click(object sender, EventArgs e) 
    { 
     string file = "D:\\backup.sql"; 
     //string conn = "server=localhost;user=root;pwd=qwerty;database=test;"; 
     String str = @"server=192.168.1.219;database=abc;userid=sha;password='123';"; 
     MySqlBackup mb = new MySqlBackup(str); 
     mb.ExportInfo.FileName = file; 
     mb.Export(); 
    } 

我的堆棧跟蹤如下 -

A first chance exception of type 'System.NullReferenceException' occurred in MySqlBackup.dll 
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unhandled exception</Description><AppDomain>TestAppMysqlDBConnect.vshost.exe</AppDomain><Exception><ExceptionType>System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Object reference not set to an instance of an object.</Message><StackTrace> at MySql.Data.MySqlClient.MySqlBackup.ExportExecute() 
    at MySql.Data.MySqlClient.MySqlBackup.Export() 
    at TestAppMysqlDBConnect.Form1.button2_Click(Object sender, EventArgs e) in C:\Users\Shashika\Documents\Visual Studio 2010\Projects\TestAppMysqlDBConnect\TestAppMysqlDBConnect\Form1.cs:line 52 
    at System.Windows.Forms.Control.OnClick(EventArgs e) 
    .. 

但是有一個例外,並說,有一個空引用異常。當我通過C#程序將數據傳遞給數據庫時。它被成功插入沒有例外。這個異常只發生在當我嘗試通過C#程序備份數據庫時。我使用了上面鏈接的2個Dll文件。這些是 - MySql.Data.dll MySqlBackup.dll

我無法解決此異常。請幫忙。

Exception

+0

請包括*文本副本的堆棧跟蹤* 。它顯示在右下角的框架中。 – 2013-02-18 05:53:11

+0

@pst - 我想我現在收錄它 – Shashika 2013-02-18 06:14:40

+0

@pst - 很抱歉。我認爲這是你想要的。不是嗎? – Shashika 2013-02-18 07:00:56

回答

0

嘗試使用此功能(不要忘記,你必須MySqlBackup.dll添加到您的項目引用):

public void Backup() 
    { 
     try 
     { 
      // 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"; 
      string 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(); 
        } 
       } 
      } 
      //Done---- 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Error , unable to backup!" + ex.Message); 
     } 
    } 
相關問題