Process Failure

I'm attempting to run the below SQL Server BCP command as a Process, but I'm getting the error, "Unhandled Exception: System.ComponentModel.Win32Exception: The system cannot find the file specified."  What could be causing this?  It runs fine using "Process.Start(Environment.GetEnvironmentVariable("COMSPEC")," /c bcp...", but I want it to wait before exiting; hence, the need for a Process.

Process p = new Process();
p.StartInfo.FileName = "bcp MyDB.dbo.MyTable in MyFile -m 1 -c -S MyServer -U MyUserId -P MyPassword";
p.Start();
p.WaitForExit();
arichexeAsked:
Who is Participating?
 
Thandava VallepalliConnect With a Mentor Commented:

try this one

      System.Diagnostics.Process proc = new System.Diagnostics.Process();
      proc.EnableRaisingEvents=false;
      proc.StartInfo.FileName = "bcp";
      proc.StartInfo.Arguments = "MyDB.dbo.MyTable in MyFile -m 1 -c -S MyServer -U MyUserId -P MyPassword";
      proc.Start();

itsvtk
0
 
dunglaCommented:
FileName property must point to a file name with full path and not including parameter
0
 
arichexeAuthor Commented:
How do I specify the parameters?
0
 
Thandava VallepalliCommented:
Here is the sample program


================
using System;

class Demo
{
      public static void Main(string[] args)
      {
            if( args != null && args.Length > 0 )
            {
                  System.Diagnostics.Process proc = new System.Diagnostics.Process();
                  proc.EnableRaisingEvents=false;
                  proc.StartInfo.FileName = args[0];
                  proc.StartInfo.Arguments = (args.Length > 1 ? args[1] : "");
                  proc.Start();
                  proc.WaitForExit();
            }else
            {
                  Console.WriteLine( "Please provide 1 or 2 parameters \n Ex: process notepad (or) process calc" );
            }
      }
}
==============================

compile command: csc <filename.cs>
Execute command:  filename.exe  <processfilepath>  <arguments to the process>

itsvtk
0
All Courses

From novice to tech pro — start learning today.