使用mysqldump的mysql备份输出空文件

时间:2016-02-05 02:04:40

标签: mysql vb.net mysqldump

我想在Vb.net中备份MySQL数据库。我使用此代码:

Dim myProcess As New Process()
myProcess.StartInfo.FileName = "cmd.exe"
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.WorkingDirectory = "c:\bu\"
myProcess.StartInfo.RedirectStandardInput = True
myProcess.StartInfo.RedirectStandardOutput = True

myProcess.Start()
Dim myStreamWriter As StreamWriter = myProcess.StandardInput
Dim mystreamreader As StreamReader = myProcess.StandardOutput
myStreamWriter.WriteLine("mysqldump --user=root --password=MYPASSWORD --host=localhost MYDATABASE > ""C:\bu\1.sql""")
myStreamWriter.Close()
myProcess.WaitForExit()
myProcess.Close()

这就是创建一个文件,但文件总是空的......

1 个答案:

答案 0 :(得分:1)

根据我的经验,我记得cmd命令不允许输入密码。尝试使用此代码在输出中获取错误消息:

    Dim myProcess As New Process()
    myProcess.StartInfo.FileName = "cmd.exe"
    myProcess.StartInfo.WorkingDirectory = mysql_path + "\bin"
    myProcess.StartInfo.UseShellExecute = False
    myProcess.StartInfo.RedirectStandardInput = True
    myProcess.StartInfo.RedirectStandardOutput = True
    myProcess.StartInfo.RedirectStandardError = True

    myProcess.Start()
    Dim myStreamWriter As StreamWriter = myProcess.StandardInput
    Dim mystreamreader As StreamReader = myProcess.StandardOutput
    Dim mystreamerror As StreamReader = myProcess.StandardError
    myStreamWriter.WriteLine(String.Format("mysqldump --user=root --password=MYPASSWORD --host=localhost MYDATABASE > ""C:\bu\1.sql""")
    myStreamWriter.WriteLine(String.Format("exit"))
    myStreamWriter.Close()
    Dim msg As String = mystreamerror.ReadToEnd()
    mystreamerror.Close()
    If msg.Length > 0 Then
        MsgBox(msg)
    End If
    myProcess.WaitForExit()
    myProcess.Close()

如果问题来自设置密码,您可以检查this article以在选项文件中设置密码。