Home > Enterprise >  C# sqlite how to print affected rows from executed sqlite command line .read
C# sqlite how to print affected rows from executed sqlite command line .read

Time:10-06

I have a file named query.sql containing a TRANSACTION, and I want to execute that file using sqlite command line from my C# code. Its works fine, but I cannot get the affected rows from the command line output. I want to get the affected rows and show it using a MessageBox.

My C# code using Process :

ProcessStartInfo startinfo = new ProcessStartInfo("sqlite3.exe");
startinfo.Arguments = string.Format("\"{0}\" \".read '{1}'\"",DatabasePath, Path.Combine(Path.GetTempPath(), "query.sql"));
//startinfo.WindowStyle = ProcessWindowStyle.Normal;
Process.Start(startinfo);

view from commandline :

sqlite3.exe "C:\Users\me\AppData\Local\app\database.db" ".read 'C:\Users\me\AppData\Local\Temp\query.sql'"

example of my sql file:

I executed the command and the db got changed, but I cannot capture the affected rows. How to capture the affected rows from that Process?

CodePudding user response:

First you need to add Select Changes(); to the end of your SQL.

https://www.sqlite.org/lang_corefunc.html#changes

Then you need to setup your process to redirect the output to your app.

startinfo.UseShellExecute = false;
startinfo.RedirectStandardOutput = true;
var proc = Process.Start(startinfo);
while (!proc.StandardOutput.EndOfStream)
{
    string output = proc.StandardOutput.ReadLine();
    Console.WriteLine(output);
}

https://stackoverflow.com/a/4291965/7182460

While the above should work there is an easier way. I'm not sure what your reason is for using the command line. You might want to take a look at using an ORM.

https://github.com/praeclarum/sqlite-net

  • Related