execute Sqlite3 command line tool from code

Viewed 533

I'm trying to run the sqlite.exe tool as a process in my c# code in order to read a sql from a file.

If I run the sqLite3 tool in powershell then it works fine (sqlite3.exe "mydatabase.db" ".read mySql.sql")

But when I run the sqlite3 tool from my c# code as a process, then nothing happens to mydatabase.db. It's still 0b when sqlite3 terminates. I get no error message, the output from the sqlite3.exe is an empty string and the exit code is 1 (verified in the exit event). Does anyone have a clue why the database.db why the records in the .sql file is not added to the .db file?.

using (Process pProcess = new Process())
        {
            pProcess.StartInfo.FileName = sqlLite3ExePath;
            pProcess.StartInfo.Arguments = $"\"{sqLitePath2}\" \".read {sqlPath}\"";;
            pProcess.StartInfo.UseShellExecute = false;
            pProcess.StartInfo.RedirectStandardOutput = true;
            pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;//System.Diagnostics.ProcessWindowStyle.Hidden;
            pProcess.StartInfo.CreateNoWindow = false;//true; //not diplay a windows
            pProcess.EnableRaisingEvents = true;
            pProcess.Exited += PProcess_Exited;
            pProcess.Start();
            string output = pProcess.StandardOutput.ReadToEnd(); //The output result
            pProcess.WaitForExit();

            Debug.WriteLine(output);
        }
1 Answers

As CaiusJard said in the comments, an error was passed in the error stream. Adding the following lines told me that my path was wrong.

pProcess.StartInfo.RedirectStandardError = true;
string error = pProcess.StandardError.ReadToEnd(); //The output result

The path divider "\" was removed since the path was parsed twice. Once setting the argument, and once when it was read by the tool. Replacing "\" with "/" in my paths did the trick

Related