147 lines
4.0 KiB
C#
147 lines
4.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using SQLite;
|
|
using WindowsFormsApp1.server;
|
|
|
|
namespace WindowsFormsApp1.db
|
|
{
|
|
public class SQLiteHelper<T> where T : new()
|
|
{
|
|
|
|
//private readonly string _databasePath = Path.Combine(Environment.CurrentDirectory, "db\\MES_PLC.db");
|
|
private readonly string _databasePath = "C:\\Users\\bnala\\source\\repos\\WindowsFormsApp1\\WindowsFormsApp1\\db\\MES_PLC.db";
|
|
private readonly SQLiteConnection _connection; // SQLite连接对象
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
public SQLiteHelper()
|
|
{
|
|
// 创建SQLite连接对象并打开连接
|
|
_connection = new SQLiteConnection(_databasePath);
|
|
_connection.CreateTable<T>(); // 如果表不存在,则创建该表[不会创建重复的表]
|
|
}
|
|
|
|
/// <summary>
|
|
/// 数据插入
|
|
/// </summary>
|
|
/// <param name="item">要插入的数据项</param>
|
|
/// <returns></returns>
|
|
public BllResult Insert(T item)
|
|
{
|
|
if(_connection.Insert(item) > 0)
|
|
{
|
|
return BllResult.Sucess();
|
|
}
|
|
else
|
|
{
|
|
return BllResult.Error();
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 数据删除
|
|
/// </summary>
|
|
/// <param name="id">要删除的数据的主键ID</param>
|
|
/// <returns></returns>
|
|
public BllResult Delete(int id)
|
|
{
|
|
if (_connection.Delete<T>(id) > 0)
|
|
{
|
|
return BllResult.Sucess();
|
|
}
|
|
else
|
|
{
|
|
return BllResult.Error();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 数据更新
|
|
/// </summary>
|
|
/// <param name="item">要更新的数据项</param>
|
|
/// <returns></returns>
|
|
public BllResult Update(T item)
|
|
{
|
|
if(_connection.Update(item) > 0)
|
|
{
|
|
return BllResult.Sucess();
|
|
}
|
|
else {
|
|
return BllResult.Error();
|
|
}
|
|
}
|
|
|
|
public BllResult UpdateAll(List<T> item)
|
|
{
|
|
if (_connection.UpdateAll(item) == item.Count)
|
|
{
|
|
return BllResult.Sucess();
|
|
}
|
|
else
|
|
{
|
|
return BllResult.Error();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据条件查询记录
|
|
/// </summary>
|
|
/// <param name="predExpr">查询条件</param>
|
|
/// <returns></returns>
|
|
public BllResult<List<T>> Query(Expression<Func<T, bool>> predExpr)
|
|
{
|
|
var result = _connection.Table<T>().Where(predExpr).ToList();
|
|
if (result.Count > 0 )
|
|
{
|
|
return BllResult<List<T>>.Sucess(result);
|
|
}
|
|
else
|
|
{
|
|
return BllResult<List<T>>.Error();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询所有数据
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public BllResult<List<T>> QueryAll()
|
|
{
|
|
var result = _connection.Table<T>().ToList();
|
|
if (result.Count > 0)
|
|
{
|
|
return BllResult<List<T>>.Sucess(result);
|
|
}
|
|
else
|
|
{
|
|
return BllResult<List<T>>.Error();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据条件查询单条记录
|
|
/// </summary>
|
|
/// <param name="predExpr">查询条件</param>
|
|
/// <returns></returns>
|
|
public BllResult<T> QuerySingle(Expression<Func<T, bool>> predExpr)
|
|
{
|
|
var result = _connection.Table<T>().Where(predExpr).FirstOrDefault();
|
|
if (result != null)
|
|
{
|
|
return BllResult<T>.Sucess(result);
|
|
}
|
|
else
|
|
{
|
|
return BllResult<T>.Error(result);
|
|
}
|
|
}
|
|
}
|
|
}
|