forked from yanw/App_win_iot_V2.0
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
345 lines
13 KiB
345 lines
13 KiB
using IOTContainer.SQLite;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace IOTContainer.Common
|
|
{
|
|
public class LocalStorage
|
|
{
|
|
/// <summary>
|
|
/// Sqlite数据库路径
|
|
/// </summary>
|
|
private static string _dbFilePath = AppDomain.CurrentDomain.BaseDirectory + @"\QMSql\localstorage.qm";
|
|
|
|
/// <summary>
|
|
/// 检查数据库文件是否存在
|
|
/// </summary>
|
|
public static void CheckDBFile()
|
|
{
|
|
if (!File.Exists(_dbFilePath))
|
|
{
|
|
if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\QMSql"))
|
|
{
|
|
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + @"\QMSql");
|
|
}
|
|
var stream = File.Create(_dbFilePath);
|
|
stream.Close();
|
|
stream.Dispose();
|
|
|
|
CreateInfoTable();
|
|
|
|
Log.MyLog.WriteLogFile("数据库文件不存在" + Environment.NewLine + _dbFilePath, "SqliteLog");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建Info表
|
|
/// </summary>
|
|
public static bool CreateInfoTable()
|
|
{
|
|
try
|
|
{
|
|
using (var adapter = new SQLiteAdapter(_dbFilePath, null))
|
|
{
|
|
adapter.Connection();
|
|
|
|
using (var actuator = adapter.GetActuator())
|
|
{
|
|
actuator.Execute("CREATE TABLE infos(key text primary key ,value text)");
|
|
Log.MyLog.WriteLogFile("成功创建Info表" + Environment.NewLine + _dbFilePath, "SqliteLog");
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.MyLog.WriteLogFile("创建Info表错误" + Environment.NewLine + ex.ToString(), "SqliteLogError");
|
|
return false;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 创建项目表
|
|
/// </summary>
|
|
/// <param name="pipelineName">项目名称</param>
|
|
public static bool CreatePipTable(string pipelineName)
|
|
{
|
|
try
|
|
{
|
|
if (!IsHasTable(pipelineName))
|
|
{
|
|
using (var adapter = new SQLiteAdapter(_dbFilePath, null))
|
|
{
|
|
adapter.Connection();
|
|
|
|
using (var actuator = adapter.GetActuator())
|
|
{
|
|
actuator.Execute("CREATE TABLE " + pipelineName + "(key text primary key ,value text)");
|
|
Log.MyLog.WriteLogFile("成功创建" + pipelineName + "表", "SqliteLog");
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
Log.MyLog.WriteLogFile("创建失败存在表 tableName:" + pipelineName, "SqliteLog");
|
|
return false;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.MyLog.WriteLogFile("创建" + pipelineName + "表错误" + Environment.NewLine + ex.ToString(), "SqliteLogError");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 插入数据
|
|
/// </summary>
|
|
/// <param name="pipelineName">表名</param>
|
|
/// <param name="key">主键</param>
|
|
/// <param name="value">值</param>
|
|
public static bool InsertPipTable(string pipelineName, string key, string value)
|
|
{
|
|
try
|
|
{
|
|
if (!IsHasPrimaryKey(pipelineName, key))
|
|
{
|
|
using (var adapter = new SQLiteAdapter(_dbFilePath))
|
|
{
|
|
adapter.Connection();
|
|
using (var actuator = adapter.GetActuator())
|
|
{
|
|
var parameters = new Dictionary<string, object>();
|
|
parameters.Add("key", key);
|
|
parameters.Add("value", value);
|
|
actuator.Insert(pipelineName, parameters);
|
|
}
|
|
}
|
|
Log.MyLog.WriteLogFile("成功插入" + pipelineName + "表," + "key:" + key + "value:" + value, "SqliteLog");
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
using (var adapter = new SQLiteAdapter(_dbFilePath))
|
|
{
|
|
adapter.Connection();
|
|
using (var actuator = adapter.GetActuator())
|
|
{
|
|
var parameters = new Dictionary<string, object>();
|
|
parameters.Add("value", value);
|
|
actuator.Update(pipelineName, parameters, "key", key);
|
|
}
|
|
}
|
|
Log.MyLog.WriteLogFile("成功更新" + pipelineName + "表," + "key:" + key + "value:" + value, "SqliteLog");
|
|
return true;
|
|
//Log.MyLog.WriteLogFile("插入" + pipelineName + "表," + "key:" + key + "value:" + value + "失败,主键存在", "SqliteLog");
|
|
//return false; ;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.MyLog.WriteLogFile("插入" + pipelineName + "表," + "key:" + key + "value:" + value + "失败" + Environment.NewLine + ex.ToString(), "SqliteLogError");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否存在主键
|
|
/// </summary>
|
|
/// <param name="tableName">表名</param>
|
|
/// <param name="key">主键</param>
|
|
/// <returns></returns>
|
|
public static bool IsHasPrimaryKey(string tableName, string key)
|
|
{
|
|
try
|
|
{
|
|
using (var adapter = new SQLiteAdapter(_dbFilePath))
|
|
{
|
|
adapter.Connection();
|
|
using (var actuator = adapter.GetActuator())
|
|
{
|
|
using (var result = actuator.Select("select value from " + tableName + " where key='" + key + "'"))
|
|
{
|
|
if (result.Rows.Count > 0)
|
|
return true;
|
|
else
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.MyLog.WriteLogFile("判断主键是否唯一错误 table:" + tableName + "key:" + key + Environment.NewLine + ex.ToString(), "SqliteLogError");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查找
|
|
/// </summary>
|
|
/// <param name="pipelineName">项目名称</param>
|
|
/// <param name="key">主键</param>
|
|
/// <returns></returns>
|
|
public static string SelectPipTable(string pipelineName, string key)
|
|
{
|
|
try
|
|
{
|
|
using (var adapter = new SQLiteAdapter(_dbFilePath))
|
|
{
|
|
adapter.Connection();
|
|
using (var actuator = adapter.GetActuator())
|
|
{
|
|
using (var result = actuator.Select("select value from " + pipelineName + " where key='" + key + "'"))
|
|
{
|
|
Log.MyLog.WriteLogFile("查找成功 table:" + pipelineName + "key:" + key + "value:" + result, "SqliteLog");
|
|
return result.Rows[0]["value"].ToString();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.MyLog.WriteLogFile("查找异常 table:" + pipelineName + "key:" + key + Environment.NewLine + ex.ToString(), "SqliteLogError");
|
|
return "";
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查找表内全数据
|
|
/// </summary>
|
|
/// <param name="pipelineName">项目名称</param>
|
|
/// <returns></returns>
|
|
public static DataTable SelectPipTable(string pipelineName)
|
|
{
|
|
try
|
|
{
|
|
using (var adapter = new SQLiteAdapter(_dbFilePath))
|
|
{
|
|
adapter.Connection();
|
|
using (var actuator = adapter.GetActuator())
|
|
{
|
|
var result = actuator.Select("select * from " + pipelineName);
|
|
Log.MyLog.WriteLogFile("查找成功 table:" + pipelineName, "SqliteLog");
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.MyLog.WriteLogFile("查找异常 table:" + pipelineName + Environment.NewLine + ex.ToString(), "SqliteLogError");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static string SelectH5Ve()
|
|
{
|
|
try
|
|
{
|
|
using (var adapter = new SQLiteAdapter(_dbFilePath))
|
|
{
|
|
string ver = string.Empty;
|
|
adapter.Connection();
|
|
using (var actuator = adapter.GetActuator())
|
|
{
|
|
using (var result = actuator.Select("select * from H5Ve"))
|
|
{
|
|
Log.MyLog.WriteLogFile("查找H5Ve成功 ", "SqliteLog");
|
|
|
|
for (int i = 0; i < result.Rows.Count; i++)
|
|
{
|
|
if (i == result.Rows.Count - 1)
|
|
{
|
|
string key = result.Rows[i]["key"].ToString();
|
|
string value = result.Rows[i]["value"].ToString();
|
|
ver = ver + "\"" + key + "\":\"" + value + "\"";
|
|
}
|
|
else
|
|
{
|
|
string key = result.Rows[i]["key"].ToString();
|
|
string value = result.Rows[i]["value"].ToString();
|
|
ver = ver + "\"" + key + "\":\"" + value + "\",";
|
|
}
|
|
}
|
|
|
|
return "{" + ver + " }";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.MyLog.WriteLogFile("查找异常 H5Ve" + Environment.NewLine + ex.ToString(), "SqliteLogError");
|
|
return "";
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除
|
|
/// </summary>
|
|
/// <param name="pipelineName">项目名称</param>
|
|
/// <param name="key">主键</param>
|
|
public static bool DelPipRow(string pipelineName, string key)
|
|
{
|
|
try
|
|
{
|
|
using (var adapter = new SQLiteAdapter(_dbFilePath))
|
|
{
|
|
adapter.Connection();
|
|
using (var actuator = adapter.GetActuator())
|
|
{
|
|
actuator.Execute("delete from " + pipelineName + " where key ='" + key + "'");
|
|
Log.MyLog.WriteLogFile("删除成功 table:" + pipelineName + "key:" + key, "SqliteLog");
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.MyLog.WriteLogFile("删除异常 table:" + pipelineName + "key:" + key + Environment.NewLine + ex.ToString(), "SqliteLogError");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否存在表
|
|
/// </summary>
|
|
/// <param name="tableName"></param>
|
|
/// <returns></returns>
|
|
public static bool IsHasTable(string tableName)
|
|
{
|
|
try
|
|
{
|
|
using (var adapter = new SQLiteAdapter(_dbFilePath))
|
|
{
|
|
adapter.Connection();
|
|
|
|
using (var actuator = adapter.GetActuator())
|
|
{
|
|
var result = actuator.Select("SELECT COUNT(*) FROM sqlite_master where type='table' and name='" + tableName + "'");
|
|
var count = Convert.ToInt32(result.Rows[0]["COUNT(*)"]);
|
|
Log.MyLog.WriteLogFile("tablename:" + tableName + "是否存在:" + count.ToString(), "SqliteLog");
|
|
if (count == 0)
|
|
return false;
|
|
else
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.MyLog.WriteLogFile("判断存在表异常 tablename:" + tableName + Environment.NewLine + ex.ToString(), "SqliteLogError");
|
|
return false;
|
|
}
|
|
}
|
|
public static void DropData()
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|
|
|