I am running sqlplus command in c# by creating Process. Before running oracle script, I want to make sure if sqlplus is installed and working on system, so I am executing below code.
string DBConnection = $"sqlplus {databaseData.userName}/{password}@{databaseData.ServerName}";
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.StandardInput.WriteLine(DBConnection);
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
string output = cmd.StandardOutput.ReadToEnd();
Above code giving me below output in variable.
Microsoft Windows [Version 10.0.19044.3208] (c) Microsoft Corporation. All rights reserved.
C:......\bin\Debug>sqlplus {{Username}}/{{password}}@{{serverName}}
SQL*Plus: Release 11.2.0.3.0 Production on Mon Aug 28 15:30:11 2023
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options
C:......\bin\Debug>
Now to verify, I am searching for "Connected" or "ERROR" word in output variable, and deciding state of sqlplus on system.
I know this is not correct way. I went through couple of articles, but didn't get something which can give me a flag (1 or 0) or (TRUE/FALSE) which can tell me if system has sqlplus installed.
I have also tried below command,
string temp1 = $"@( echo whenever sqlerror exit failure & echo connect {{Username}}/{{password}}@{{serverName}} & echo select * from dual; & echo exit) | sqlplus.exe -s /nolog";
string temp2 = "echo %ERRORLEVEL%";
This command is providing me 1 or 0, but it also has strings which I don't require. Basically I need 1 or 0 only from below screen-shot, but its giving entire output as string,
Let me know if there is a way which can provide me a single flag.