views:

577

answers:

1

Hi according to my last question here I try to write a sql Editor or some thing like this,in this way I try to connect to CMD from C# and execute my command. now my problem is for example I connect to SQLPLUS after that I cant get SQLPLUS command,and the other resource I review don't satisfy me.Please help me how after I connected to Sqlplus ,I can a live my process to run my sql command? right now I use this code:

//Create process
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();

//strCommand is path and file name of command to run
pProcess.StartInfo.FileName = strCommand;

//strCommandParameters are parameters to pass to program
pProcess.StartInfo.Arguments = strCommandParameters;

pProcess.StartInfo.UseShellExecute = false;

//Set output of program to be written to process output stream
pProcess.StartInfo.RedirectStandardOutput = true;

//Optional
pProcess.StartInfo.WorkingDirectory = strWorkingDirectory;

//Start the process
pProcess.Start();

//Get program output
string strOutput = pProcess.StandardOutput.ReadToEnd();

//Wait for process to finish
pProcess.WaitForExit();

but i customize it.I separate the initialize, i mean i just create process object one time,but I still have problem. to run the second command I use these codes for second time calling:

pProcess.StartInfo.FileName = strCommand;

//strCommandParameters are parameters to pass to program
pProcess.StartInfo.Arguments = strCommandParameters;
//Start the process
pProcess.Start();

//Get program output
string strOutput = pProcess.StandardOutput.ReadToEnd();

//Wait for process to finish
pProcess.WaitForExit();

Thanks in advance

+1  A: 

Your question is a little confusing but I think i see your problem. First you should check out this blog post to see common issues with System.Diagnostics.Process. Your code happens to violate one that isn't listed there. The reuse of the Process object itself.

You need to refactor the code like:

    class MyProcessStarter
    {
        private ProcessStartInfo _startInfo = new ProcessStartInfo();
        public MyProcessStarter(string exe, string workingDir)
        {
            _startInfo.WorkingDirectory = workingDir;
            _startInfo.FileName = exe;
            _startInfo.UseShellExecute = false;
            _startInfo.RedirectStandardOutput = true;
        }

        public string Run(string arguments)
        {
            _startInfo.Arguments = arguments;
            Process p = Process.Start(_startInfo);
            p.Start();
            string strOutput = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            return strOutput;
        }
    }

I've written a more complete and accurate implementation called ProcessRunner. The following demonstrates it's usage to perform the same operation:

using CSharpTest.Net.Processes;
partial class Program
{
    static int Main(string[] args)
    {

        ProcessRunner run = new ProcessRunner("svn.exe");
        run.OutputReceived += new ProcessOutputEventHandler(run_OutputReceived);
        return run.Run("update", "C:\\MyProject");
    }

    static void run_OutputReceived(object sender, ProcessOutputEventArgs args)
    {
        Console.WriteLine("{0}: {1}", args.Error ? "Error" : "Output", args.Data);
    }
}
csharptest.net
Thanks dude,the first code was useful but still i have error!!!do u know ,my process run like this:I translate it to exec command:sqlplus user/pass@dbsqlplus prompt "amir"; <== this line provide error!I just sqlplus running one time,and after it i just send argument and execute it...and second utility I got error!I recieve null exception pointer and also I try to use it's own testRunFile ,but on that one also same.I cant find "Assert" class anywhere??! :((help me plzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz.
rima