1.封装SqliteHelper类
public class SqliteHelper
{
#region Fields
/// <summary>
/// 连接字符串
/// </summary>
private static string connStr = "Data Source={0};Version=3;BinaryGUID=false;";
/// <summary>
/// 密码
/// </summary>
private static string pwd = "@Ta&nSu7855qdg$%o12!34#56!7/.89!";
/// <summary>
/// 创建表命令
/// </summary>
private static string cmdCreateTable = "CREATE TABLE IF NOT EXISTS OutPutInStorageInfoLog (ID integer NOT NULL PRIMARY KEY,Number text,GoodsType text, CreateTime TEXT,Status INT);";
private static object lockObj = new object();
#endregion
#region Connection
/// <summary>
/// 连接(DB文件不存在,则会自动创建)
/// </summary>
/// <param name="filePath">本地db文件路径</param>
public static void Connection(string filePath)
{
connStr = string.Format(connStr, filePath);
if (!File.Exists(filePath))
{
SQLiteConnection.CreateFile(filePath);
using (SQLiteConnection connection = new SQLiteConnection(connStr))
{
if (connection.State != System.Data.ConnectionState.Open)
connection.Open();
connection.ChangePassword(pwd);
List<TransModel> models = new List<TransModel>();
models.Add(new TransModel { CmdText = cmdCreateTable });
ExecTransaction(models);//一个事务:如果表不存在则创建
}
}
}
#endregion
#region ExecTransaction
/// <summary>
/// 执行事务,如果出现异常则回滚
/// </summary>
private static bool ExecTransaction(List<TransModel> models)
{
using (SQLiteConnection con = new SQLiteConnection(connStr))
{
try
{
con.SetPassword(pwd);//解密
if (con.State != System.Data.ConnectionState.Open)
con.Open();
using (SQLi