我需要将SQLite数据库转储到另一个数据库文件中。为此,我计划使用SQLite Command Shell,因为我真的很想避免使用ADO.NET。我需要启动的脚本非常简单:
sqlite3 "C:\path\to\input.db" .dump | sqlite3 "C:\path\to\dump.db"
(sqlite3
这里是SQLite Command Shell可执行文件)
首先,要检查一切是如何进行的,我在cmd.exe
进行了测试。它工作正常,没有控制台输出,并在几秒钟内完成,非常好。
现在,我想要一个C#方法来做同样的事情。这是我的工作:
public void Dump(string sqlitePath, string inputPath, string dumpPath)
{
Process sqlite = new Process
{
StartInfo =
{
UseShellExecute = true,
FileName = sqlitePath,
Arguments = $"\"{inputPath}\" .dump | \"{sqlitePath}\" \"{dumpPath}\""
}
};
sqlite.Start();
sqlite.WaitForExit();
}
现在,这导致命令行窗口显示其中包含sqlite3
输出。
此输出基本如下:
...
INSERT INTO "myTable" VALUES(1117,64,45.0,-104.0);
INSERT INTO "myTable" VALUES(1118,55,45.0,-103.0);
INSERT INTO "myTable" VALUES(1119,42,45.0,-102.0);
INSERT INTO "myTable" VALUES(1120,42,45.0,-101.0);
INSERT INTO "myTable" VALUES(1121,42,45.0,-100.0);
INSERT INTO "myTable" VALUES(1122,38,45.0,-99.0);
INSERT INTO "myTable" VALUES(1123,47,45.0,-98.0);
INSERT INTO "myTable" VALUES(1124,40,45.0,-97.0);
INSERT INTO "myTable" VALUES(1125,37,45.0,-96.0);
INSERT INTO "myTable" VALUES(1126,34,45.0,-95.0);
...
这显然是转储日志。现在,这有副作用:当以这种方式运行时,操作需要永远。我认为这是因为打印的输出占用了大部分时间。
现在,问题是:
Process.Start
运行应用程序时会获得输出,而在使用cmd.exe
运行应用程序时什么也得不到?cmd.exe
执行相同的行为?更新1
此外,我尝试通过执行cmd.exe
而不是sqlite3
来达到此bevahiour,以便我的代码如下所示:
public void Dump(string sqlitePath, string inputPath, string dumpPath)
{
Process sqlite = new Process
{
StartInfo =
{
UseShellExecute = true,
FileName = "cmd.exe",
Arguments = $"\"{sqlitePath}\" \"{inputPath}\" .dump | \"{sqlitePath}\" \"{dumpPath}\""
}
};
sqlite.Start();
sqlite.WaitForExit();
}
这会导致显示cmd窗口而没有输出。它指向一些文件夹(与此处使用的文件无关),基本上看起来没有传递任何参数(它接受用户输入等)。它也永远不会关闭。我是否在某种程度上滥用cmd
语法?
更新2
使用它:
public void Dump(string sqlitePath, string inputPath, string dumpPath)
{
Process sqlite = new Process
{
StartInfo =
{
UseShellExecute = true,
FileName = "cmd.exe",
Arguments = $"/c \" \"{sqlitePath}\" \"{inputPath}\" .dump | \"{sqlitePath}\" \"{dumpPath}\" \""
}
};
sqlite.Start();
sqlite.WaitForExit();
}
注意/c
和参数列表中的额外引号。