我有一個名為 query.sql 的檔案,其中包含一個 TRANSACTION,我想從我的 C# 代碼中使用 sqlite 命令列執行該檔案。它作業正常,但我無法從命令列輸出中獲取受影響的行。我想獲取受影響的行并使用 MessageBox 顯示它。
我的 C# 代碼使用 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);
從命令列查看:
sqlite3.exe "C:\Users\me\AppData\Local\app\database.db" ".read 'C:\Users\me\AppData\Local\Temp\query.sql'"

我執行了命令并且資料庫發生了變化,但我無法捕獲受影響的行。如何從該行程中捕獲受影響的行?
uj5u.com熱心網友回復:
首先,您需要添加Select Changes();到 SQL 的末尾。
https://www.sqlite.org/lang_corefunc.html#changes
然后,您需要設定流程以將輸出重定向到您的應用程式。
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
雖然上述應該有效,但有一種更簡單的方法。我不確定您使用命令列的原因是什么。您可能想看看使用 ORM。
https://github.com/praeclarum/sqlite-net
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/311426.html
上一篇:SqliteCommand.ExecuteScalar()回傳null(C#5.0和System.Data.Sqlite)
