WPF 使用sqlite3.exe 进行数据库的快速备份和还原
sqlite下载
官方网站
using System;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
namespace sqloperate
{
class Program
{
static void Main(string[] args)
{
//Console.WriteLine("Start!");
//string testData = @"E:\\test\\PQESDataTest.db*E:\\test\\D.sql";
//var argsTemp = testData.Split("*");
if(args[0] == "Import")
{
//导入数据库表
SqlImport(args);
}
else if (args[0] == "Export")
{
//导出数据库表
SqlExport(args);
}
}
/// <summary>
/// 数据库导出 args[0]判断是导入/导出 args[1]数据库路径,args[2]sql文件存放路径
/// </summary>
public static void SqlExport(string[] args)
{
try
{
//新进程
Process p = new Process();
//设置要启动的应用程序
p.StartInfo.FileName = "cmd.exe";
//是否使用操作系统shell启动
p.StartInfo.UseShellExecute = false;
// 接受来自调用程序的输入信息
p.StartInfo.RedirectStandardInput = true;
//输出信息
p.StartInfo.RedirectStandardOutput = true;
// 输出错误
//p.StartInfo.RedirectStandardError = true;
//不显示程序窗口
p.StartInfo.CreateNoWindow = false;
//启动程序
p.Start();
//当前sqlite.exe所在路径
var sqlitePath = Path.Combine(Directory.GetCurrentDirectory(), "sqloperate\\sqlite3.exe");
//向cmd窗口发送输入信息
p.StandardInput.WriteLine(string.Format("{0} {1} .dump > {2}", sqlitePath,args[1],args[2]));
p.StandardInput.WriteLine("exit");
p.StandardInput.AutoFlush = true;
//获取输出信息
string strOuput = p.StandardOutput.ReadToEnd();
//等待程序执行完退出进程
p.WaitForExit();
p.Close();
Console.WriteLine(strOuput);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/// <summary>
/// 数据库导入 args[0]判断是导入/导出 args[1]数据库路径,args[2]sql文件存放路径
/// </summary>
public static void SqlImport(string[] args)
{
try
{
//新进程
Process p = new Process();
//设置要启动的应用程序
p.StartInfo.FileName = "cmd.exe";
//是否使用操作系统shell启动
p.StartInfo.UseShellExecute = false;
// 接受来自调用程序的输入信息
p.StartInfo.RedirectStandardInput = true;
//输出信息
p.StartInfo.RedirectStandardOutput = true;
// 输出错误
//p.StartInfo.RedirectStandardError = true;
//不显示程序窗口
p.StartInfo.CreateNoWindow = false;
//启动程序
p.Start();
//当前sqlite.exe所在路径
var sqlitePath = Path.Combine(Directory.GetCurrentDirectory(), "sqloperate\\sqlite3.exe");
//向cmd窗口发送输入信息
p.StandardInput.WriteLine(string.Format("{0} {1} < {2}", sqlitePath, args[1], args[2]));
p.StandardInput.WriteLine("exit");
//p.StandardInput.AutoFlush = true;
//获取输出信息
string strOuput = p.StandardOutput.ReadToEnd();
//等待程序执行完退出进程
p.WaitForExit();
p.Close();
Console.WriteLine(strOuput);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}