using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Newtonsoft.Json;
namespace HH.WCS.Mobox3.HaiCheng.Util.Helper
{
///
/// 数据库连接帮助类
///
public static class AdoSqlHelper where T : class, new()
{
///
/// 根据条件查询一条数据
///
/// 条件
///
public static T QueryFirst(Expression> condition)
{
var db = new SqlHelper().GetInstance();
// 返回数据
return db.Queryable().First(condition);
}
///
/// 根据条件查询多条数据
///
///
public static List QueryList(Expression> condition)
{
var db = new SqlHelper().GetInstance();
// 返回数据
return db.Queryable().Where(condition).ToList();
}
///
/// 根据条件查询对应数据数量
///
/// 条件
///
public static int QueryCount(Expression> condition)
{
var db = new SqlHelper().GetInstance();
// 返回数据
return db.Queryable().Count(condition);
}
///
/// 根据条件修改一条数据
///
/// 需要进行修改的对象
/// 修改条件
///
public static bool UpdateFirst(T model, Expression> condition)
{
var db = new SqlHelper().GetInstance();
// 修改数据
var executeCommand = db.Updateable(model).UpdateColumns(condition).ExecuteCommand();
if (executeCommand > 0)
{
return true;
}
else
return false;
}
///
/// 根据条件修改多条数据
///
///
///
///
public static int UpdateList(List models, Expression> condition)
{
var db = new SqlHelper().GetInstance();
// 修改数据
return db.Updateable(models).UpdateColumns(condition).ExecuteCommand();
}
///
/// 新增一条数据
///
///
///
public static bool AddFirst(T model)
{
var db = new SqlHelper().GetInstance();
var executeCommand = db.Insertable(model).ExecuteCommand();
if (executeCommand > 0)
return true;
else
return false;
}
///
/// 新增多条数据
///
///
///
public static int AddList(List models)
{
var db = new SqlHelper().GetInstance();
var executeCommand = db.Insertable(models).ExecuteCommand();
return executeCommand;
}
///
/// 删除一条数据
///
/// 需要删除的对象
///
public static bool DeleteFirst(T model)
{
var db = new SqlHelper().GetInstance();
var executeCommand = db.Deleteable(model).ExecuteCommand();
if (executeCommand > 0)
return true;
else
return false;
}
///
/// 删除多条数据
///
/// 需要删除的对象集合
///
public static int DeleteList(List models)
{
var db = new SqlHelper().GetInstance();
return db.Deleteable(models).ExecuteCommand();
}
///
/// 根据条件删除数据
///
/// 条件
///
public static int DeleteList(Expression> condition)
{
var db = new SqlHelper().GetInstance();
return db.Deleteable().Where(condition).ExecuteCommand();
}
///
/// 按照时间倒序查询
///
///
///
///
public static T QueryFirstByDecs(Expression> condition, Expression> orderBy)
{
var db = new SqlHelper().GetInstance();
// 返回数据
return db.Queryable().OrderByDescending(orderBy).First(condition);
}
}
}