using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
namespace iMES.Bi.Data
{
///
/// 公共接口
///
public class BaseEFDaoJW : IBaseDao where T : class, new() //限制T为class
{
public SqlSugarClient Db;//用来处理事务多表查询和复杂的操作
public SimpleClient CurrentDb { get { return new SimpleClient(Db); } }//用来处理T表的常用操作
public static string ConnectionString = Appsettings.app(new string[] { "Connection", "DbConnectionString" });//获取连接字符串
public static string DBType = Appsettings.app(new string[] { "Connection", "DBType" });//获取连接字符串
public BaseEFDaoJW()
{
Db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = ConnectionString,
DbType = DBType == "MySql" ? SqlSugar.DbType.MySql : SqlSugar.DbType.SqlServer,
InitKeyType = InitKeyType.SystemTable,//从特性读取主键和自增列信息
IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了
});
// 调式代码 用来打印SQL
Db.Aop.OnLogExecuting = (sql, pars) =>
{
//WriteLOG(sql + "\r\n" + Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
};
}
public void CModel()
{
Db.DbFirst.CreateClassFile("E:\\Demo", "iMES.Bi.Data");
}
public virtual IEnumerable GetALLEntities()
{
//AsNoTracking不记录数据变化状况
return CurrentDb.GetList();
}
public virtual IEnumerable GetEntities(Expression> exp)
{
//AsNoTracking不记录数据变化状况
return CurrentDb.GetList(exp).ToList();
}
///
/// 根据条件查找
///
/// lambda查询条件where
///
public virtual T GetEntity(Expression> exp)
{
return CurrentDb.GetList(exp).SingleOrDefault();
}
///
/// 获取所有Entity(立即执行请使用ToList()
///
/// Sql语句
/// 可变参数
///
public virtual IEnumerable GetEntities(string CommandText)
{
return Db.SqlQueryable("select * from " + typeof(T).Name + " where 1=1 and " + CommandText).ToList();
}
///
/// 插入Entity
///
///
///
public virtual bool Insert(T entity)
{
entity= Db.Insertable(entity).ExecuteReturnEntity();
//int dataID = CurrentDb.InsertReturnIdentity(entity);
//List List = Db.DbMaintenance.GetIsIdentities(entity.GetType().Name);
//if (List.Count > 0)
//{
// //如果有自增,赋值
// entity.GetType().GetProperty(List[0].ToString()).SetValue(entity, dataID, null);
//}
return true;
}
///
/// 同时插入多个实体。
///
///
///
public virtual bool Insert(IEnumerable entities)
{
int Return = 0;
if (entities.Count() > 0)
{
Return = Db.Insertable(entities.ToArray()).ExecuteCommand();
}
return Return > 0;
}
///
/// 更新Entity(注意这里使用的傻瓜式更新,可能性能略低)
///
///
///
public virtual bool Update(T entity)
{
return CurrentDb.Update(entity);
}
///
/// 删除Entity
///
///
///
public virtual bool Delete(T entity)
{
return CurrentDb.Delete(entity);
}
///
/// 批量删除Entity
///
///
///
public virtual bool Delete(Expression> exp)
{
return CurrentDb.Delete(exp);
}
///
/// 根据条件查找
///
/// Sql语句
/// 可变参数
///
public virtual DataTable GetDTByCommand(string CommandText)
{
return Db.Ado.GetDataTable(CovSQL(CommandText));
}
///
/// 获取链接字符串
///
/// Sql语句
/// 可变参数
///
public string GetDBString()
{
return Db.CurrentConnectionConfig.ConnectionString;
}
//执行SQL语句
public void ExsSql(string sql)
{
List parameters = null;
Db.Ado.ExecuteCommand(CovSQL(sql), parameters);
}
public object ExsSclarSql(string sql)
{
List parameters = null;
return Db.Ado.GetString(CovSQL(sql), parameters);
}
///
/// 替换
///
///
///
private string CovSQL(string strSQL)
{
if (Db.CurrentConnectionConfig.DbType == SqlSugar.DbType.MySql)//MYSQL数据库
{
strSQL = strSQL.Replace("isnull", "ifnull").Replace("ISNULL", "IFNULL");
}
return strSQL;
}
///
/// 数据分页
///
/// 表名
/// 字段
/// 默认20
/// 页数
/// 排序
/// 可选
/// 总数
///
public DataTable GetDataPager(string viewName, string fieldName, int pageSize, int pageNo, string orderString, string whereString, ref int recordTotal)
{
string strSQL = "select " + fieldName + " from " + viewName + " where " + whereString;
DataTable dt = Db.SqlQueryable