views:

40

answers:

1

Hi I try to write a program like a compiler.In this case I must simulate these codes of SQLPL(This one can be just for example).for example in command prompt i wana do these:

c:\> sqlplus
 ....
Enter User-Name:(here we must enter username) xxxx
Enter password:(same up)yyyyy
...
sql>(now i want to send my sql command to shell)prompt "rima";
"rima"  [<-output]
sql> prompt "xxxx"
sql> exit

very simple. I try to use this code:

ProcessStartInfo psi = new ProcessStartInfo(
@"sqlplus");
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
//psi.RedirectStandardError = true;
Process process = new Process();
process.StartInfo = psi;
bool started = process.Start();
if (started)
{
process.StandardInput.WriteLine("user/password");
process.StandardInput.Flush();
string ret = process.StandardOutput.ReadLine(); // <-- stalls here
System.Console.WriteLine("sqlplus says :" + ret + "\".");
}

i find out it form here but as if u read this code has problem!DEADLOCK Condition problem! this is my second time i ask my question,every time my knowledge growing,but i cant get how I can solve my problem at last!!!

So I kindly kiss your hand,please help me,these process is not clear for me.any one can give me a code that handle my problem?Already i look at all codes,also I try to use ProcessRunner that in my last post some one offer me,but I cant use it also,i receive an error.

I hope by first example u find out what i want,and solve the second code problem...
thanks
I use C# for implemantation,also my DB is Oracle
I dont want close sqlplus for each time connecting be cauz it take time to log in again

A: 

You approach is not working for this example in my opinion. You wrote:

Enter User-Name:(here we must enter username) xxxx
Enter password:(same up)yyyyy

So it looks as if there is no newline involved. Since you are connecting to StandardOutput, which is buffered it will get only flushed when a newline character appears or a flush() is called directly. This is probably not the case (should be happen in sqlplus).

So as long as you can not change sqlplus and insert a flush or newline there, a synchronous read will not help you at all. Try to communicate asynchronous, like:

process.OutputDataReceived += OnOutputDataReceived;
process.Start();
process.BeginOutputReadLine();
// ... more missing code here ...

But this is a far more complex approach ...

tanascius
Also we can get the user and pass like xxxx/yyyyy to sqlplus.in this case what's ur idea?
rima
Sending is not a problem, you will send a newline after the username/password. But receiving is your problem. imho you have to go with a asynchronous solution to find out what input was asked and what responses you got
tanascius