using Dapper; using Dapper.Contrib.Extensions; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Text; namespace IStation.DataProvider { /// /// 数据库访问基类 /// public partial class BaseMsSqlDAL where T : class, new() { /// /// 构造函数 /// public BaseMsSqlDAL() { this.TableName = EntityHelper.GetTableName(); this.PrimaryKey = EntityHelper.GetPrimaryKey(); } /// /// 对象的表名 /// public virtual string TableName { get; set; } /// /// 对象主键属性名称 /// public virtual string PrimaryKey { get; set; } /// /// 数据库连接 /// protected virtual IDbConnection Connection { get { var connection = new SqlConnection(ConfigHelper.Db4Scada); connection.Open(); return connection; } } #region Insert /// /// 插入指定对象到数据库中 /// /// 指定的对象 /// public virtual long Insert(T entity) { using (IDbConnection dbConnection = Connection) { return dbConnection.Insert(entity); } } /// /// 插入指定对象集合到数据库中 /// /// 指定的对象集合 /// public virtual bool Inserts(IEnumerable list) { bool result = false; using (IDbConnection dbConnection = Connection) using (IDbTransaction trans = dbConnection.BeginTransaction()) { result = dbConnection.Insert(list, trans) > 0; trans.Commit(); } return result; } #endregion #region Update /// /// 更新对象属性到数据库中 /// /// 指定的对象 /// public virtual bool Update(T entity) { bool result = false; using (IDbConnection dbConnection = Connection) { result = dbConnection.Update(entity); } return result; } /// /// 更新指定对象集合到数据库中 /// /// 指定的对象集合 /// public virtual bool Updates(IEnumerable list) { bool result = false; using (IDbConnection dbConnection = Connection) using (IDbTransaction trans = dbConnection.BeginTransaction()) { result = dbConnection.Update(list, trans); trans.Commit(); } return result; } #endregion #region Delete /// /// 从数据库中删除指定对象 /// /// 指定的对象 /// public virtual bool Delete(T entity) { bool result = false; using (IDbConnection dbConnection = Connection) { result = dbConnection.Delete(entity); } return result; } /// /// 从数据库中删除指定对象集合 /// /// 指定的对象集合 /// public virtual bool Deletes(IEnumerable list) { bool result = false; using (IDbConnection dbConnection = Connection) using (IDbTransaction trans = dbConnection.BeginTransaction()) { result = dbConnection.Delete(list, trans); trans.Commit(); } return result; } /// /// 根据指定对象的ID,从数据库中删除指定对象 /// /// 对象的ID /// public virtual bool DeleteByID(long id) { bool result = false; using (IDbConnection dbConnection = Connection) { string strSql = string.Format("DELETE FROM {0} WHERE {1} = @id", TableName, PrimaryKey); var parameters = new DynamicParameters(); parameters.Add("@id", id); result = dbConnection.Execute(strSql, parameters) > 0; } return result; } /// /// 通过id列表删除 /// /// /// public virtual bool DeleteByIds(IEnumerable ids) { using (IDbConnection dbConnection = Connection) { var strSql = string.Format("DELETE FROM {0} WHERE {1} IN @ids", TableName, PrimaryKey); var parameters = new DynamicParameters(); parameters.Add("@ids", ids); var result = dbConnection.Execute(strSql, parameters) > 0 ? true : false; return result; } } /// /// 从数据库中删除所有对象 /// /// public virtual bool DeleteAll() { bool result = false; using (IDbConnection dbConnection = Connection) { result = dbConnection.DeleteAll(); } return result; } #endregion #region Query /// /// 返回数据库所有的对象集合 /// /// public virtual IEnumerable GetAll() { using (IDbConnection dbConnection = Connection) { return dbConnection.GetAll(); } } /// /// 查询数据库,返回指定ID的对象 /// /// 主键的值 /// public virtual T GetByID(long id) { using (IDbConnection dbConnection = Connection) { return dbConnection.Get(id); } } /// /// 通拓id列表获取 /// /// public virtual IEnumerable GetByIds(IEnumerable ids) { using (IDbConnection dbConnection = Connection) { var strSql = string.Format("SELECT * FROM {0} WHERE {1} IN @ids", TableName, PrimaryKey); var parameters = new DynamicParameters(); parameters.Add("@ids", ids); return dbConnection.Query(strSql, parameters); } } #endregion } }