在process.start上使用mysqldump没有任何反应,但是当我在cmd上尝试参数时它正在工作

时间:2021-02-25 20:45:16

I'm spending hours on troubleshooting this. I have this code

我花了几个小时来解决这个问题。我有这个代码

    Dim drive_name As String = Directory.GetCurrentDirectory & "\database"

    Dim db_name As String = "sample_db.sql"

    Dim argument As String = "-u root sample_db table1 > """ & drive_name & "\" & db_name & """"
    Try
        Diagnostics.Process.Start("C:\xampp\mysql\bin\mysqldump.exe", argument)
        Console.WriteLine("Argument:  " & argument)
    Catch ex As Exception
        Console.WriteLine("Error:   " & ex.ToString)
    End Try

By looking at the console.writeline, it output like this

通过查看console.writeline,它输出如下

-u root sample_db table1 > "C:\Users\MyName\Desktop\MyProjectPath\bin\Debug\database\sample_db.sql"

But by checking C:\Users\MyName\Desktop\MyProjectPath\bin\Debug\database\ I can't see sample_db.sql, so I open mysqdump copy the console.writeline and paste it to cmd. I now have this on mysqldump

但通过检查C:\ Users \ MyName \ Desktop \ MyProjectPath \ bin \ Debug \ database \我看不到sample_db.sql,所以我打开mysqdump复制console.writeline并将其粘贴到cmd。我现在在mysqldump上有这个

C:\Users\MyName>C:\xampp\mysql\bin\mysqldump -u root sample_db table1 > "C:\Users\MyName\Desktop\MyProjectPath\bin\Debug\database\sample_db.sql"

By executing that on mysqldump it works.

通过在mysqldump上执行它可以工作。

How do I fix this?

我该如何解决?

3 个解决方案

#1


1  

While this doesnt use mysql (as I dont have it) the principal applies.

虽然这不使用mysql(因为我没有),校长适用。

Process p = new Process();
p.StartInfo.FileName = @"c:\windows\system32\ipconfig.exe";
p.StartInfo.Arguments = @"/all";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
String output = p.StandardOutput.ReadToEnd();
File.WriteAllText(@"c:\log.txt",output);
p.WaitForExit();

in log.txt was my output as expected.

在log.txt中我的输出是预期的。

so in your case, you have the mysqldump command in filename, arguments in arguments, and change the writealltext to your output file

所以在你的情况下,你有文件名中的mysqldump命令,参数中的参数,并将writealltext更改为输出文件

Obviously you can then use this to be far more creative should you desire, size for example maybe an issue, after all if you have gb of data, storing it all in one string is possibly asking a bit much, but the output is a stream, so realistically you could work further and work with the stream more directly and output chunk by chunk

显然你可以根据需要使用它更有创意,例如大小可能是一个问题,毕竟如果你有数据gb,将它全部存储在一个字符串中可能要求一点,但输出是一个流,实际上你可以进一步工作并更直接地使用流并按块输出块

#2


0  

In this link a user suggested to change the > to -r

在此链接中,用户建议将>更改为-r

So instead of

而不是

Dim argument As String = "-u root sample_db table1 > """ & drive_name & "\" & db_name & """"

You will now have

你现在会有

 Dim argument As String = "-u root sample_db table1 -r """ & drive_name & "\" & db_name & """"

And it will work!

它会工作!

#3


0  

Base on the suggestion of BugFinder I am now able to use standardInput and standardOutput`

根据BugFinder的建议,我现在可以使用standardInput和standardOutput`

Code for import:

进口代码:

    Try
        Dim myProcess As New Diagnostics.Process()
        myProcess.StartInfo.FileName = "cmd.exe"
        myProcess.StartInfo.UseShellExecute = False
        myProcess.StartInfo.WorkingDirectory = "C:\xampp\mysql\bin\"
        myProcess.StartInfo.RedirectStandardInput = True
        myProcess.StartInfo.RedirectStandardOutput = True
        myProcess.Start()
        Dim myStreamWriter As StreamWriter = myProcess.StandardInput
        Dim mystreamreader As StreamReader = myProcess.StandardOutput
        createDatabase()
        myStreamWriter.WriteLine("mysql -u  root  sample_db < " & drive_name & "\" & db_name)
        myStreamWriter.Close()
        myProcess.WaitForExit()
        myProcess.Close()
    Catch ex As Exception
        Console.WriteLine("Error on Import: " & ex.ToString)
    End Try

If you want to change it to import, simply change this line of code to your desired mysqldump export command:

如果要将其更改为导入,只需将此行代码更改为所需的mysqldump export命令:

myStreamWriter.WriteLine("mysql -u  root  sample_db < " & drive_name & "\" & db_name)

Example of export command

导出命令的示例

myStreamWriter.WriteLine("mysql -u  root  sample_db > " & drive_name & "\" & db_name)

#1


1  

While this doesnt use mysql (as I dont have it) the principal applies.

虽然这不使用mysql(因为我没有),校长适用。

Process p = new Process();
p.StartInfo.FileName = @"c:\windows\system32\ipconfig.exe";
p.StartInfo.Arguments = @"/all";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
String output = p.StandardOutput.ReadToEnd();
File.WriteAllText(@"c:\log.txt",output);
p.WaitForExit();

in log.txt was my output as expected.

在log.txt中我的输出是预期的。

so in your case, you have the mysqldump command in filename, arguments in arguments, and change the writealltext to your output file

所以在你的情况下,你有文件名中的mysqldump命令,参数中的参数,并将writealltext更改为输出文件

Obviously you can then use this to be far more creative should you desire, size for example maybe an issue, after all if you have gb of data, storing it all in one string is possibly asking a bit much, but the output is a stream, so realistically you could work further and work with the stream more directly and output chunk by chunk

显然你可以根据需要使用它更有创意,例如大小可能是一个问题,毕竟如果你有数据gb,将它全部存储在一个字符串中可能要求一点,但输出是一个流,实际上你可以进一步工作并更直接地使用流并按块输出块

#2


0  

In this link a user suggested to change the > to -r

在此链接中,用户建议将>更改为-r

So instead of

而不是

Dim argument As String = "-u root sample_db table1 > """ & drive_name & "\" & db_name & """"

You will now have

你现在会有

 Dim argument As String = "-u root sample_db table1 -r """ & drive_name & "\" & db_name & """"

And it will work!

它会工作!

#3


0  

Base on the suggestion of BugFinder I am now able to use standardInput and standardOutput`

根据BugFinder的建议,我现在可以使用standardInput和standardOutput`

Code for import:

进口代码:

    Try
        Dim myProcess As New Diagnostics.Process()
        myProcess.StartInfo.FileName = "cmd.exe"
        myProcess.StartInfo.UseShellExecute = False
        myProcess.StartInfo.WorkingDirectory = "C:\xampp\mysql\bin\"
        myProcess.StartInfo.RedirectStandardInput = True
        myProcess.StartInfo.RedirectStandardOutput = True
        myProcess.Start()
        Dim myStreamWriter As StreamWriter = myProcess.StandardInput
        Dim mystreamreader As StreamReader = myProcess.StandardOutput
        createDatabase()
        myStreamWriter.WriteLine("mysql -u  root  sample_db < " & drive_name & "\" & db_name)
        myStreamWriter.Close()
        myProcess.WaitForExit()
        myProcess.Close()
    Catch ex As Exception
        Console.WriteLine("Error on Import: " & ex.ToString)
    End Try

If you want to change it to import, simply change this line of code to your desired mysqldump export command:

如果要将其更改为导入,只需将此行代码更改为所需的mysqldump export命令:

myStreamWriter.WriteLine("mysql -u  root  sample_db < " & drive_name & "\" & db_name)

Example of export command

导出命令的示例

myStreamWriter.WriteLine("mysql -u  root  sample_db > " & drive_name & "\" & db_name)