2008-10-10 115 views

回答

1

你可以調用mysqldump,但是你可能需要在Mysql服務器上運行你的VB.NET。

0

您可以讀取每個表的數據並將其寫入新數據庫。

0

我會寫一個存儲過程,因爲MySQL 5支持它們,它處理所有數據「繁重」的工作。然後,只需創建每個「晚上」調用過程的計劃任務。對於後面的組件,我強烈推薦Powershell ....它很棒。

1

我發現最簡單的方法是使用mysqldump.exe這是一個獨立的應用程序。

mysqldump --host=[HOSTNAME] --user=[USER] --password=[PASSWORD] -R [DATABASE NAME] > [PATH TO BACKUP FILE] 

我們有問題與備份不保存數據庫的功能,但是-R開關整理這麼ID建議,如果你使用你的數據庫的存儲過程或函數中使用它。

改爲使用mysql命令來恢復創建的文件。

mysql --host=[HOSTNAME] --user=[USER] --password=[PASSWORD] [DATABASE NAME] < [PATH TO BACKUP FILE] 
7

您可以使用MySqlBackup.NET,這是mysqldump的一個替代品。

官方網站&文檔>http://mysqlbackupnet.codeplex.com

實例:

備份MySQL數據庫

Dim conn As MySqlConnection = New MySqlConnection(constr) 
Dim cmd As MySqlCommand = New MySqlCommand 
cmd.Connection = conn 
conn.Open 
Dim mb As MySqlBackup = New MySqlBackup(cmd) 
mb.ExportToFile("C:\backup.sql") 
conn.Close 

還原MySQL數據庫

Dim conn As MySqlConnection = New MySqlConnection(constr) 
Dim cmd As MySqlCommand = New MySqlCommand 
cmd.Connection = conn 
conn.Open 
Dim mb As MySqlBackup = New MySqlBackup(cmd) 
mb.ImportFromFile("C:\backup.sql") 
conn.Close 

我是這個項目的作者之一。

+0

我不斷收到此錯誤:在System.Windows.Forms中發生未處理的異常類型'System.IO.FileLoadException'。dll 其他信息:無法加載文件或程序集「MySqlBackup,Version = 2.0.9.2,Culture = neutral,PublicKeyToken = null」或其某個依賴項。一個強命名的程序集是必需的。 (來自HRESULT的異常:0x80131044) – 2016-11-01 19:37:22

0

這就是我用來備份mysql數據。我製作了mysqldump.exe和mysql.exe的副本,並將其保存在我的LIB_PATH上,然後下面的代碼將備份您的數據。您可以指定您的mysqldump.exe目錄並將其分配給LIB_PATH,在參數下提供您的登錄詳細信息,然後指定您的輸出目錄,我的設置爲BACKUP_DIR,並使用預格式化的Now()作爲我的文件名。代碼非常簡單。 Goodluck

Using myProcess As New Process() 
     Dim newfiledb As String = BACKUPDIR_PATH & Format(Now(), "[email protected]~mm_tt").ToString & "_local.sql" 
     Try 
      myProcess.StartInfo.FileName = "mysqldump.exe" 
      myProcess.StartInfo.WorkingDirectory = LIB_PATH 
      myProcess.StartInfo.Arguments = "--host=localhost --user=username --password=yourpassword yourdatabase -r " & newfiledb 
      myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden 
      myProcess.Start() 
      myProcess.WaitForExit() 
      MsgBox("Backup Created ... " & vbNewLine & newfiledb) 
     Catch ex As Exception 
      MsgBox(ex.Message, vbCritical + vbOKOnly, ex.Message) 
     Finally 
      myProcess.Close() 
     End Try 
    End Using 
1

使用此代碼。 它適合我。

我有這樣一個問題,然後發現這篇文章

http://www.experts-exchange.com/Programming/Languages/.NET/Q_27155602.html

的例子是在C#。我手動將其轉換爲vb.net並添加轉換爲'utf8'。

Imports System.Text 
Public Class Form1 
    Dim OutputStream As System.IO.StreamWriter 
    Sub OnDataReceived1(ByVal Sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) 
     If e.Data IsNot Nothing Then 
      Dim text As String = e.Data 
      Dim bytes As Byte() = Encoding.Default.GetBytes(text) 
      text = Encoding.UTF8.GetString(bytes) 
      OutputStream.WriteLine(text) 
      End If 
    End Sub 

    Sub CreateBackup() 
     Dim mysqldumpPath As String = "d:\mysqldump.exe" 
     Dim host As String = "localhost" 
     Dim user As String = "root" 
     Dim pswd As String = "Yourpwd" 
     Dim dbnm As String = "BaseName" 
     Dim cmd As String = String.Format("-h{0} -u{1} -p{2} {3}", host, user, pswd, dbnm) 
     Dim filePath As String = "d:\backup\fieName.sql" 
     OutputStream = New System.IO.StreamWriter(filePath, False, System.Text.Encoding.UTF8) 

     Dim startInfo As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo() 
     startInfo.FileName = mysqldumpPath 
     startInfo.Arguments = cmd 

     startInfo.RedirectStandardError = True 
     startInfo.RedirectStandardInput = False 
     startInfo.RedirectStandardOutput = True 
     startInfo.UseShellExecute = False 
     startInfo.CreateNoWindow = True 
     startInfo.ErrorDialog = False 

     Dim proc As System.Diagnostics.Process = New System.Diagnostics.Process() 
     proc.StartInfo = startInfo 
     AddHandler proc.OutputDataReceived, AddressOf OnDataReceived1 
     proc.Start() 
     proc.BeginOutputReadLine() 
     proc.WaitForExit() 

     OutputStream.Flush() 
     OutputStream.Close() 
     proc.Close() 
    End Sub 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As  System.EventArgs) Handles MyBase.Load 

     CreateBackup() 

    End Sub 
    End Class