2

When I run the following from cmd it runs ok

>mysql -h 134.86.157.132 -u sas vm < c:\vm.sql

When I try to do the same from code it does not work

ProcessStartInfo info = new ProcessStartInfo("mysql");
info.Arguments = @"-h 134.86.157.132 -u sas vm < c:\vm.sql";
info.Domain = "134.86.157.132";
info.UserName = "sas";
info.Arguments = @"vm < c:\vm.sql";
info.UseShellExecute = false;
Process.Start(info);

What am I doing wrong here? It does not work and I get some wrong password exception?

Edit:

I run it like this now

            ProcessStartInfo info = new ProcessStartInfo("mysql");
            info.UseShellExecute = false;
            info.RedirectStandardInput = true;
            info.RedirectStandardOutput = true;
            info.RedirectStandardError = true;
            info.Arguments = @"-h 134.86.157.132 -u sas vm < c:\vm.sql";
            Process.Start(info);

and get following error The system cannot find the file specified

SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
Night Walker
  • 20,638
  • 52
  • 151
  • 228

4 Answers4

3

Try it like this:

ProcessStartInfo info = new ProcessStartInfo("mysql");
info.Arguments = @"-h 134.86.157.132 -u sas vm < c:\vm.sql";
info.UseShellExecute = false;
Process.Start(info);
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
3

"-h 134.86.157.132 -u sas vm" are the arguments, don't use domain and username.

You also need to redirect the standard input stream to pass your vm.sql file in. See this example: ProcessStartInfo.RedirectStandardInput Property

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
2

You're setting UseShellExecute to false, but it's the shell that interprets the "<" part to redirect stdin from a file.

Unless there's a specific reason why you need UseShellExecute to be false, set it to true. Alternatively, redirect standard input for the new process, and give it the data directly.

EDIT: As specified in other answers, keep the original arguments, and don't set Domain/UserName on the ProcessStartInfo. Those refer to Windows usernames and domains, not MySQL ones.

At that point you'll be able to set UseShellExecute to true with no ill effects, with any luck.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • when i put it to true i get following error The Process object must have the UseShellExecute property set to false in order to start a process as a user. – Night Walker Dec 08 '10 at 15:54
  • @Night: Fair enough... so do you *definitely* have to start the process as that user? I suspect not, given that it's unlikely to be a *windows* user. See Marc's answer. – Jon Skeet Dec 08 '10 at 15:56
0

How about something like from this post:

        Process p = new Process();
        ProcessStartInfo info = new ProcessStartInfo();
        info.FileName = "cmd.exe";
        info.RedirectStandardInput = true;
        info.UseShellExecute = false;

        p.StartInfo = info;
        p.Start();

        using (StreamWriter sw = new StreamWriter(p.StandardInput))
        {
            if (sw.BaseStream.CanWrite)
            {
                sw.WriteLine("mysql -h 134.86.157.132 -u sas vm < c:\vm.sql");
            }
        }
Community
  • 1
  • 1
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184