Cary Bondoc
Cary Bondoc

Reputation: 2988

using mysqldump on process.start nothing happens, but when I try the argument on cmd it is working

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

-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>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.

How do I fix this?

Upvotes: 1

Views: 582

Answers (3)

Cary Bondoc
Cary Bondoc

Reputation: 2988

Base on the suggestion of BugFinder I am now able to use standardInput and 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:

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)

Upvotes: 1

BugFinder
BugFinder

Reputation: 17858

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

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.

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

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

Upvotes: 1

Cary Bondoc
Cary Bondoc

Reputation: 2988

In this link a user suggested to change the > to -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!

Upvotes: 0

Related Questions