如何在vb.net中备份我的sql数据库

时间:2012-12-16 23:03:11

标签: mysql sql vb.net

我希望我的vb应用程序的用户能够将数据库(MySQL)备份和还原到存储介质上。我的问题是我不想在代码中指定'c:\因为我希望应用程序能够找到哑文件,无论它是否在驱动器c上创建。下面是我使用的代码但是当我在另一台机器上安装它时,它在D:上有它的窗口和程序文件。事实证明,我必须检查每台机器的驱动器号,在我发布应用程序之前在代码中更改它以允许备份,我不想这样做。我希望它具有普遍性。因此转储文件是否在驱动程序C,G或其他任何内容上。任何帮助。下面是我使用的代码。     Dim cmd As String

Private Sub cmdBackup_Click()
    Screen.MousePointer = vbHourglass
    DoEvents

    cmd = Chr(34) & "C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqldump" & Chr(34) & " -uroot -psecretpswd --routines --comments db_name > c:\MyBackup.sql"
    Call execCommand(cmd)

    Screen.MousePointer = vbDefault
    MsgBox "done"
End Sub

3 个答案:

答案 0 :(得分:1)

有一个名为MySqlBackup.NET的编译DLL。实际上它是MySqlDump的替代品。

功能

  • 导出/导入表的结构&行
  • 导出/导入存储过程,函数,触发器,事件,视图
  • 自定义表格和行导出。
  • 能够对流程应用加密。
  • 导出BLOB并另存为文件。
  • 在导入过程中收集SQL语法错误。
  • 导出/导入将报告进度。启用进度条的使用。
  • 能够以同步或异步模式执行。
  • 导出/导入Zip文件。

有关详细信息,请参阅以下链接

已修改:已添加代码示例

备份MySql数据库

Dim con As String = "server=localhost;user=root;pwd=1234;database=test;"
Dim file As String = "C:\backup.sql"
Dim mb As New MySqlBackup(con)
mb.ExportInfo.FileName = file
mb.Export()

恢复MySql数据库

Dim con As String = "server=localhost;user=root;pwd=1234;database=test;"
Dim file As String = "C:\backup.sql"
Dim mb As New MySqlBackup(con)
mb.ImportInfo.FileName = file
mb.Import()

答案 1 :(得分:0)

通常,此命令是使用应用程序外部的参数构建的,而不是MySqlDump的硬编码路径,数据库名称和目标文件夹的路径。

您的代码应更改为此类

Private Sub cmdBackup_Click()
    Screen.MousePointer = vbHourglass
    DoEvents
    Dim mySqlDumpCmd = ConfigurationManager.AppSettings("PathToMySqlDump")
    Dim dbName = ConfigurationManager.AppSettings("DatabaseToBackup")
    Dim destPath = ConfigurationManager.AppSettings("DestinationPath")
    cmd = Chr(34) & mySqlDumpCmd & Chr(34) & " -uroot -psecretpswd --routines --comments " +
          dbName & " > " & destPath
    Call execCommand(cmd)
    Screen.MousePointer = vbDefault
    MsgBox "done"
End Sub

并且您的application.config文件包含这些值

<?xml version="1.0"?>
<configuration>
    <configSections>
    .......

    <appSettings>
    <add key="PathToMySqlDump" value="C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqldump.exe"/>
    <add key="DatabaseToBackup" value="db_name"/>
    <add key="DestinationPath" value="C:\MyBackup.sql"/>
    </appSettings>
    .......

通过这种方式,您可以从应用程序的配置文件中读取密钥信息。如果需要,您可以轻松更改命令使用的信息,而无需触及应用程序中的任何内容

答案 2 :(得分:0)

使用此代码。它对我有用。

我遇到了这样的问题然后找到了这篇文章

&#34; http://www.experts-exchange.com/Programming/Languages/.NET/Q_27155602.html&#34;

示例是在C#中。我手动将其转换为vb.net并将转换添加到&#39; utf8&#39;。

   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