0

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,

enter image description here

Let me know if there is a way which can provide me a single flag.

Keval Patel
  • 161
  • 1
  • 15
  • You can try using Exit code from process https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.exitcode?view=net-7.0 – Heinz Siahaan Aug 28 '23 at 10:14
  • No, ExitCode is giving 0, if command run successfully, I am looking for sqlplus command status. – Keval Patel Aug 28 '23 at 11:21
  • What are you trying to accomplish? See if sqlplus is installed? Or whether you can connect to a particular database with it? – Paul W Aug 28 '23 at 15:16
  • Added 1 screen shot for more clarity on requirement, I am trying to execute this command and require to verify, if users credentials are correct or not. – Keval Patel Aug 29 '23 at 05:23

0 Answers0