Pārlūkot izejas kodu

xls:数据相关

xls 6 gadi atpakaļ
vecāks
revīzija
c3b1ba9d7d

+ 455 - 0
MBI/SAGA.DotNetUtils/Data.Framework/AbstractDal.cs

@@ -0,0 +1,455 @@
+/*-------------------------------------------------------------------------
+ * 功能描述:AbstracDal
+ * 作者:xulisong
+ * 创建时间: 2019/2/27 9:34:38
+ * 版本号:v1.0
+ *  -------------------------------------------------------------------------*/
+
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Data;
+using System.Data.Common;
+using System.Data.SQLite;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Saga.Framework.DB
+{
+    public abstract class AbstractDal<T> where T:new ()
+    {
+        /// <summary>
+        /// 默认构造函数
+        /// </summary>
+        public AbstractDal()
+        {
+        }
+        /// <summary>
+        /// 指定表名以及主键,对基类进构造
+        /// </summary>
+        /// <param name="tableName">表名</param>
+        /// <param name="primaryKey">表主键</param>
+        public AbstractDal(string tableName, string primaryKey)
+        {
+            this.TableName = tableName;
+            this.PrimaryKey = primaryKey;
+            this.DefaultSortField = primaryKey;
+        }
+        /// <summary>
+        /// 连接字符串
+        /// </summary>
+        protected string ConnectionString { get; set; }
+        /// <summary>
+        /// 表名称
+        /// </summary>
+        protected string TableName { get; set; }
+        /// <summary>
+        /// 主件
+        /// </summary>
+        protected string PrimaryKey { get; set; }
+        /// <summary>
+        /// 参数化占位符
+        /// </summary>
+        protected string ParameterPrefix { get; set; } = "@";
+        /// <summary>
+        /// 安全的字段信息
+        /// </summary>
+        protected string SafeFieldFormat { get; set; } = "[{0}]";
+
+        protected string DefaultSortField { get; set; }
+        #region 转化相关类
+
+        public abstract Database CreateDatabase();
+        protected abstract DbParameter CreatePrimaryKeyParameter(object key);
+      
+        protected virtual T ReaderToEntity(IDataReader dr)
+        {
+            T t = Activator.CreateInstance<T>();
+            System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties();
+            System.Reflection.PropertyInfo[] array = properties;
+            for (int i = 0; i < array.Length; i++)
+            {
+                System.Reflection.PropertyInfo propertyInfo = array[i];
+                try
+                {
+                    if (dr[propertyInfo.Name].ToString() != "")
+                    {
+                        propertyInfo.SetValue(t, dr[propertyInfo.Name] ?? "", null);
+                    }
+                }
+                catch
+                {
+                }
+            }
+            return t;
+        }
+
+        protected virtual Hashtable EntityToRecord(T obj)
+        {
+            Hashtable hashtable = new Hashtable();
+            System.Reflection.PropertyInfo[] properties = obj.GetType().GetProperties();
+            for (int i = 0; i < properties.Length; i++)
+            {
+                object value = properties[i].GetValue(obj, null);
+                value = ((value == null) ? DBNull.Value : value);
+                if (!hashtable.ContainsKey(properties[i].Name))
+                {
+                    hashtable.Add(properties[i].Name, value);
+                }
+            }
+            return hashtable;
+        }
+
+        protected virtual string GetSafeFileName(string fieldName)
+        {
+            return string.Format(this.SafeFieldFormat, fieldName);
+        }
+        protected virtual string GetParameterPlaceholder(string fieldName)
+        {
+            return string.Format("{0}{1}", ParameterPrefix, fieldName);
+        }
+        /// <summary>
+        /// 获取参数相关维护
+        /// </summary>
+        /// <param name="db"></param>
+        /// <param name="recordFields"></param>
+        /// <returns></returns>
+        protected virtual List<DbParameter>  GetDbParameters(Database db, Hashtable recordFields)
+        {
+            List<DbParameter> parameters = new List<DbParameter>();
+            foreach (string fieldName in recordFields.Keys)
+            {
+                object value = recordFields[fieldName];
+                value = (value ?? DBNull.Value);
+                if (value is DateTime)
+                {
+                    if (Convert.ToDateTime(value) <= DateTime.MinValue)
+                    {
+                        value = DBNull.Value;
+                    }
+
+                    parameters.Add(db.CreateParameter(fieldName, DbType.DateTime, value));
+                }
+                else
+                {
+                    parameters.Add(db.CreateParameter(fieldName, value));
+                }
+            }
+            return parameters;
+        }
+        #endregion
+
+        #region 验证相关类
+        protected void ValidateInput(string condition)
+        {
+            if (DatabaseUtil.HasInjection(condition))
+            {
+                throw new Exception("检测出SQL注入的恶意数据:" + condition);
+            }
+        }
+        #endregion
+        #region 方法执行
+        #region 查找,查找方式有很多
+        public virtual bool ExistByKey(object key)
+        {
+            string arg = string.Format("{0} = {1}{0}", this.PrimaryKey, this.ParameterPrefix);
+            string commandText = string.Format("Select Count(*) from {0} WHERE {1} ", TableName, arg);
+            Database database = CreateDatabase();
+            return Convert.ToInt32(database.ExecuteScalar(commandText, CreatePrimaryKeyParameter(key))) > 0;
+        }
+        public virtual bool ExistByCondition(string condition)
+        {
+            ValidateInput(condition);
+            string commandText = string.Format("Select Count(*) from {0} WHERE {1} ", TableName, condition);
+            Database database = CreateDatabase();
+            return Convert.ToInt32(database.ExecuteScalar(commandText)) > 0;
+        }
+        public virtual T FindByKey(object key)
+        {
+            string condition = string.Format("{0} = {1}{0}", this.PrimaryKey, this.ParameterPrefix);
+            return FindSingle(condition, null, CreatePrimaryKeyParameter(key));
+        }
+        public virtual T FindSingle(string condition)
+        {
+            return FindSingle(condition, null);
+        }
+        public virtual T FindSingle(string condition, string orderBy)
+        {
+            return FindSingle(condition, orderBy, null);
+        }
+        public virtual T FindSingle(string condition, string orderBy, params DbParameter[] parameters)
+        {
+            ValidateInput(condition);
+            ValidateInput(orderBy);
+            T result = default(T);
+            string commandText = string.Format("Select * From {0} ", TableName);
+            if (!string.IsNullOrWhiteSpace(condition))
+            {
+                commandText += string.Format("Where {0} ", condition);
+            }
+            if (!string.IsNullOrWhiteSpace(orderBy))
+            {
+                commandText = commandText + " " + orderBy;
+            }
+            else if(!string.IsNullOrWhiteSpace(DefaultSortField))
+            {
+                commandText = commandText + "" + "order by " + DefaultSortField + " ASC";
+            }
+            Database database = CreateDatabase();
+            using (IDataReader dataReader = database.ExecuteReader(commandText, parameters))
+            {
+                if (dataReader.Read())
+                {
+                    result = this.ReaderToEntity(dataReader);
+                }
+            }
+            return result;
+        }
+
+        public virtual List<T> Find(string condition)
+        {
+            return Find(condition, null);
+        }
+        public virtual List<T> Find(string condition, string orderBy)
+        {
+            return Find(condition, orderBy, null);
+        }
+        public virtual List<T> Find(string condition, string orderBy, params DbParameter[] parameters)
+        {
+            ValidateInput(condition);
+            ValidateInput(orderBy);
+            T result = default(T);
+            string commandText = string.Format("Select * From {0} ", TableName);
+            if (!string.IsNullOrEmpty(condition))
+            {
+                commandText += string.Format("Where {0} ", condition);
+            }
+            if (!string.IsNullOrEmpty(orderBy))
+            {
+                commandText = commandText + " " + orderBy;
+            }
+            else if (!string.IsNullOrWhiteSpace(DefaultSortField))
+            {
+                commandText = commandText + "" + "Order by " + DefaultSortField + " ASC";
+            }
+            Database database = CreateDatabase();
+            List<T> list = new List<T>();
+            using (System.Data.IDataReader dataReader = database.ExecuteReader(commandText, parameters))
+            {
+                while (dataReader.Read())
+                {
+                    var item = this.ReaderToEntity(dataReader);
+                    list.Add(item);
+                }
+            }
+            return list;
+        }
+        #endregion
+
+        #region 删除
+        public virtual bool DeleteByKey(object key)
+        {
+            return DeleteByKey(key, null);
+        }
+      
+        public virtual bool DeleteByCondition(string condition)
+        {
+            return DeleteByCondition(condition, null);
+        }     
+        public virtual bool DeleteByKey(object key, DbTransaction trans)
+        {
+            string arg = string.Format("{0} = {1}{0}", this.PrimaryKey, this.ParameterPrefix);
+            string sqlText = string.Format("DELETE FROM {0} WHERE {1} ", this.TableName, arg);
+            Database database = CreateDatabase();
+            return database.ExecuteNonQuery(sqlText, CreatePrimaryKeyParameter(key)) > 0;
+        }
+        public virtual bool DeleteByCondition(string condition, DbTransaction trans)
+        {
+            bool result = false;
+            ValidateInput(condition);
+            string sqlText = string.Format("DELETE FROM {0} WHERE {1} ", this.TableName, condition);
+            Database database = CreateDatabase();
+            if (trans != null)
+            {
+                result = database.ExecuteNonQuery(sqlText, trans) > 0;
+            }
+            else
+            {
+                result = database.ExecuteNonQuery(sqlText) > 0;
+            }
+            return result;
+        }
+        #endregion
+
+        #region 添加
+        public virtual bool Insert(T t)
+        {
+            return Insert(EntityToRecord(t), null);
+        }
+        public virtual bool Insert(T t, DbTransaction trans)
+        {
+            return Insert(EntityToRecord(t), trans);
+        }
+        public virtual bool Insert(Hashtable recordFields, DbTransaction trans)
+        {
+            bool flag = false;
+            bool result;
+            if (recordFields == null || recordFields.Count < 1)
+            {
+                result = flag;
+            }
+            else
+            {
+                string intoText = "";
+                string valueText = "";
+                foreach (string fieldName in recordFields.Keys)
+                {
+                    intoText += string.Format("{0},", this.GetSafeFileName(fieldName));
+                    valueText += string.Format("{0},", this.GetParameterPlaceholder(fieldName));
+                }
+                intoText = intoText.Trim(',');
+                valueText = valueText.Trim(',');
+                string commandText = string.Format("INSERT INTO {0} ({1}) VALUES ({2})", TableName, intoText, valueText);
+                Database database = this.CreateDatabase();
+                List<DbParameter> parameters = GetDbParameters(database,recordFields);          
+                if (trans != null)
+                {
+                    flag = (database.ExecuteNonQuery(commandText, trans, parameters.ToArray()) > 0);
+                }
+                else
+                {
+                    flag = (database.ExecuteNonQuery(commandText, parameters.ToArray()) > 0);
+                }
+                result = flag;
+            }
+            return result;
+        }
+        public virtual object InsertAddGetKey(T t)
+        {
+            return InsertAddGetKey(EntityToRecord(t), null);
+        }
+        public virtual object InsertAddGetKey(T t, DbTransaction trans)
+        {
+            return InsertAddGetKey(EntityToRecord(t), trans);
+        }
+        public virtual object InsertAddGetKey(Hashtable recordFields, DbTransaction trans)
+        {
+            object result = null;
+            if (recordFields == null || recordFields.Count < 1)
+            {
+                return result;
+            }
+            else
+            {
+                string intoText = "";
+                string valueText = "";
+                foreach (string fieldName in recordFields.Keys)
+                {
+                    intoText += string.Format("{0},", this.GetSafeFileName(fieldName));
+                    valueText += string.Format("{0},", this.GetParameterPlaceholder(fieldName));
+                }
+                intoText = intoText.Trim(',');
+                valueText = valueText.Trim(',');
+                string commandText = string.Format("INSERT INTO {0} ({1}) VALUES ({2});Select LAST_INSERT_ROWID()", TableName, intoText, valueText);
+                Database database = this.CreateDatabase();
+                List<DbParameter> parameters = new List<DbParameter>();
+                foreach (string fieldName in recordFields.Keys)
+                {
+                    object value = recordFields[fieldName];
+                    value = (value ?? DBNull.Value);
+                    if (value is DateTime)
+                    {
+                        if (Convert.ToDateTime(value) <= DateTime.MinValue)
+                        {
+                            value = DBNull.Value;
+                        }
+                    }
+                    parameters.Add(database.CreateParameter(fieldName, value));
+                }
+                if (trans != null)
+                {
+                    result = database.ExecuteScalar(commandText, trans, parameters.ToArray());
+                }
+                else
+                {
+                    result = database.ExecuteScalar(commandText, parameters.ToArray());
+                }
+            }
+            return result;
+        }
+        #endregion
+
+        #region 修改
+        public virtual bool Update(T obj, object primaryKeyValue)
+        {
+            string condition = string.Format("{0} = {1}{0}", this.PrimaryKey, this.ParameterPrefix);
+            return UpdateByCondition(EntityToRecord(obj), condition, null, CreatePrimaryKeyParameter(primaryKeyValue));
+        }
+
+        public virtual bool Update(T obj, object primaryKeyValue, DbTransaction trans)
+        {
+            string condition = string.Format("{0} = {1}{0}", this.PrimaryKey, this.ParameterPrefix);
+            return UpdateByCondition(EntityToRecord(obj), condition, trans, CreatePrimaryKeyParameter(primaryKeyValue));
+        }
+        public virtual bool UpdateByCondition(T obj, string condition)
+        {
+            return UpdateByCondition(obj, condition, null);
+        }
+        public virtual bool UpdateByCondition(T obj, string condition, DbTransaction trans)
+        {
+            return UpdateByCondition(EntityToRecord(obj), condition, trans);
+        }
+
+        public virtual bool UpdateByCondition(Hashtable recordFields, string condition, DbTransaction trans, params DbParameter[] conditionParameters)
+        {
+            bool result;
+            try
+            {
+                if (recordFields == null || recordFields.Count < 1)
+                {
+                    return false;
+                }
+                else
+                {
+                    recordFields.Remove(this.PrimaryKey);
+                    if (recordFields.Count < 1)
+                    {
+                        return false;
+                    }
+                    string setText = "";
+                    foreach (string fieldName in recordFields.Keys)
+                    {
+                        setText += string.Format("{0} = {1},", this.GetSafeFileName(fieldName), this.GetParameterPlaceholder(fieldName));
+                    }
+                    string query = string.Format("UPDATE {0} SET {1} WHERE {2} ", new object[]
+                    {
+                        TableName,
+                        setText.Substring(0, setText.Length - 1),
+                        condition
+                    });
+                    Database database = this.CreateDatabase();
+                    bool flag = false;
+                    List<DbParameter> parameters = GetDbParameters(database, recordFields);
+                    parameters.AddRange(conditionParameters.ToArray());
+                    if (trans != null)
+                    {
+                        flag = (database.ExecuteNonQuery(query, trans, parameters.ToArray()) > 0);
+                    }
+                    else
+                    {
+                        flag = (database.ExecuteNonQuery(query, parameters.ToArray()) > 0);
+                    }
+                    result = flag;
+                }
+            }
+            catch (System.Exception ex)
+            {
+                throw;
+            }
+            return result;
+        }
+        #endregion
+        #endregion
+    }
+}

+ 208 - 0
MBI/SAGA.DotNetUtils/Data.Framework/Bll.cs

@@ -0,0 +1,208 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Data.Common;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Saga.Framework.DB
+{
+    public class Bll<T> where T :new()
+    {
+        protected string m_DalName = "";
+
+        /// <summary>
+        /// 数据访问层程序集的清单文件的文件名,不包括其扩展名,可使用Assembly.GetExecutingAssembly().GetName().Name
+        /// </summary>
+        protected string m_DalAssemblyName;
+
+        /// <summary>
+        /// BLL命名空间的前缀(BLL.)
+        /// </summary>
+        protected string m_BllSuffix = "Bll";
+
+        /// <summary>
+        /// 基础数据访问层接口对象
+        /// </summary>
+        protected IDal<T> m_BaseDal = null;
+
+        /// <summary>
+        /// 参数赋值后,初始化相关对象
+        /// </summary>
+        /// <param name="bllFullName">BLL业务类的全名(子类必须实现),子类构造函数传入this.GetType().FullName</param>
+        /// <param name="dalAssemblyName">数据访问层程序集的清单文件的文件名,不包括其扩展名,可使用Assembly.GetExecutingAssembly().GetName().Name</param>
+        /// <param name="bllPrefix">BLL命名空间的前缀(BLL.)</param>
+        protected void Init(string bllFullName, string dalAssemblyName, string bllSuffix = "Bll")
+        {
+            if (string.IsNullOrEmpty(bllFullName))
+            {
+                throw new ArgumentNullException("子类未设置bllFullName业务类全名!");
+            }
+            if (string.IsNullOrEmpty(dalAssemblyName))
+            {
+                throw new ArgumentNullException("子类未设置dalAssemblyName程序集名称!");
+            }
+            this.m_DalAssemblyName = dalAssemblyName;
+            this.m_BllSuffix = bllSuffix;
+            if(string.IsNullOrWhiteSpace(m_DalName))
+            {
+                m_DalName = bllFullName.Replace(m_BllSuffix, "Dal");
+            }
+            this.m_BaseDal =(IDal<T>)Assembly.Load(dalAssemblyName).CreateInstance(m_DalName) ;
+        }
+
+        /// <summary>
+        /// 调用前检查baseDal是否为空引用
+        /// </summary>
+        protected void CheckDAL()
+        {
+            if (this.m_BaseDal == null)
+            {
+                throw new ArgumentNullException(nameof(m_BaseDal), "初始化dal失败");
+            }
+        }
+
+        #region 方法执行
+        #region 查找,查找方式有很多
+        public virtual bool ExistByKey(object key)
+        {
+            this.CheckDAL();
+            return this.m_BaseDal.ExistByKey(key);
+        }
+        public virtual bool ExistByCondition(string condition)
+        {
+            this.CheckDAL();
+            return this.m_BaseDal.ExistByCondition(condition);
+        }
+        public virtual T FindByKey(object key)
+        {
+            this.CheckDAL();
+            return this.m_BaseDal.FindByKey(key);
+        }
+        public virtual T FindSingle(string condition)
+        {
+            this.CheckDAL();
+            return this.m_BaseDal.FindSingle(condition);
+        }
+        public virtual T FindSingle(string condition, string orderBy)
+        {
+            this.CheckDAL();
+            return this.m_BaseDal.FindSingle(condition, orderBy);
+        }
+        public virtual T FindSingle(string condition, string orderBy, params DbParameter[] parameters)
+        {
+            this.CheckDAL();
+            return this.m_BaseDal.FindSingle(condition, orderBy, parameters);
+        }
+
+        public virtual List<T> Find(string condition)
+        {
+            this.CheckDAL();
+            return this.m_BaseDal.Find(condition);
+        }
+        public virtual List<T> Find(string condition, string orderBy)
+        {
+            this.CheckDAL();
+            return this.m_BaseDal.Find(condition, orderBy);
+        }
+        public virtual List<T> Find(string condition, string orderBy, params DbParameter[] parameters)
+        {
+            this.CheckDAL();
+            return this.m_BaseDal.Find(condition, orderBy, parameters);
+        }
+        #endregion
+
+        #region 删除
+        public virtual bool DeleteByKey(object key)
+        {
+            this.CheckDAL();
+            return this.m_BaseDal.DeleteByKey(key);
+        }
+
+        public virtual bool DeleteByCondition(string condition)
+        {
+            this.CheckDAL();
+            return this.m_BaseDal.DeleteByCondition(condition);
+        }
+        public virtual bool DeleteByKey(object key, DbTransaction trans)
+        {
+            this.CheckDAL();
+            return this.m_BaseDal.DeleteByKey(key, trans);
+        }
+    
+        public virtual bool DeleteByCondition(string condition, DbTransaction trans)
+        {
+            this.CheckDAL();
+            return this.m_BaseDal.DeleteByCondition(condition, trans);
+        }
+        #endregion
+
+        #region 添加
+        public virtual bool Insert(T t)
+        {
+
+            this.CheckDAL();
+            return this.m_BaseDal.Insert(t);
+        }
+        public virtual bool Insert(T t, DbTransaction trans)
+        {
+
+            this.CheckDAL();
+            return this.m_BaseDal.Insert(t,trans);
+        }
+        public virtual bool Insert(Hashtable recordFields, DbTransaction trans)
+        {
+            this.CheckDAL();
+            return this.m_BaseDal.Insert(recordFields, trans);
+        }
+        public virtual object InsertAddGetKey(T t)
+        {
+            this.CheckDAL();
+            return this.m_BaseDal.InsertAddGetKey(t);
+        }
+        public virtual object InsertAddGetKey(T t, DbTransaction trans)
+        {
+            this.CheckDAL();
+            return this.m_BaseDal.InsertAddGetKey(t, trans);
+        }
+        public virtual object InsertAddGetKey(Hashtable recordFields, DbTransaction trans)
+        {
+            this.CheckDAL();
+            return this.m_BaseDal.InsertAddGetKey(recordFields, trans);
+        }
+        #endregion
+
+        #region 修改
+        public virtual bool Update(T obj, object primaryKeyValue)
+        {
+            this.CheckDAL();
+            return this.m_BaseDal.Update(obj, primaryKeyValue);
+        }
+
+        public virtual bool Update(T obj, object primaryKeyValue, DbTransaction trans)
+        {
+            this.CheckDAL();
+            return this.m_BaseDal.Update(obj, primaryKeyValue, trans);
+        }
+        public virtual bool UpdateByCondition(T obj, string condition)
+        {
+            this.CheckDAL();
+            return this.m_BaseDal.UpdateByCondition(obj, condition);
+        }
+        public virtual bool UpdateByCondition(T obj, string condition, DbTransaction trans)
+        {
+            this.CheckDAL();
+            return this.m_BaseDal.UpdateByCondition(obj, condition, trans);
+        }
+
+        public virtual bool UpdateByCondition(Hashtable recordFields, string condition, DbTransaction trans, params DbParameter[] conditionParameters)
+        {
+            this.CheckDAL();
+            return this.m_BaseDal.UpdateByCondition(recordFields, condition, trans);
+        }
+        #endregion
+        #endregion
+    }
+}

+ 186 - 0
MBI/SAGA.DotNetUtils/Data.Framework/Database.cs

@@ -0,0 +1,186 @@
+/*-------------------------------------------------------------------------
+ * 功能描述:Database
+ * 作者:xulisong
+ * 创建时间: 2019/2/27 16:09:56
+ * 版本号:v1.0
+ *  -------------------------------------------------------------------------*/
+
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Data.Common;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Saga.Framework.DB
+{
+    public abstract class Database
+    {
+        public Database(string connectionString)
+        {
+            ConnectionString = connectionString;
+        }
+
+        #region 连接相关
+
+        /// <summary>
+        /// 连接字符串
+        /// </summary>
+        public string ConnectionString { get; private set; }
+        #region 抽象方法
+        /// <summary>
+        /// 创建连接信息
+        /// </summary>
+        /// <returns></returns>
+        public abstract DbConnection CreateConnection();
+        /// <summary>
+        /// 创建关联事务
+        /// </summary>
+        /// <returns></returns>
+        public abstract DbTransaction CreateTransaction();
+        /// <summary>
+        /// 生成相关参数
+        /// </summary>
+        /// <param name="fieldName"></param>
+        /// <param name="value"></param>
+        /// <returns></returns>
+        public abstract DbParameter CreateParameter(string fieldName, object value);
+
+        public abstract DbParameter CreateParameter(string fieldName,DbType dbType, object value);
+        protected abstract DataSet InnerExecuteDataSet(DbCommand cmd); 
+        #endregion
+        #endregion
+
+        #region 通用基础方法
+
+        public T CommonExecute<T>(Func<DbCommand, T> realCore, string commandText, params DbParameter[] commandParameters)
+        {
+            T result = default(T);
+            using (DbConnection connection = CreateConnection())
+            {
+                DbCommand cmd = connection.CreateCommand();
+                cmd.CommandText = commandText;
+                foreach (var parameter in commandParameters)
+                {
+                    cmd.Parameters.Add(parameter);
+                }
+
+                result = CommonExecute(realCore, cmd);
+            }
+
+            return result;
+        }
+
+        public T CommonExecute<T>(Func<DbCommand, T> realCore, string commandText, DbTransaction transaction,
+            params DbParameter[] commandParameters)
+        {
+            DatabaseUtil.ValidateTransaction(transaction);
+            T result = default(T);
+            using (DbCommand cmd = transaction.Connection.CreateCommand())
+            {
+                cmd.Transaction = transaction;
+                cmd.CommandText = commandText;
+                result = CommonExecute(realCore, cmd);
+
+            }
+
+            return result;
+        }
+
+        public T CommonExecute<T>(Func<DbCommand, T> realCore, DbCommand cmd)
+        {
+            T result = default(T);
+            if (cmd.Connection.State == ConnectionState.Closed)
+                cmd.Connection.Open();
+            result = realCore(cmd);
+            return result;
+        }
+
+        #endregion
+
+        #region  增删查改执行
+
+        public int ExecuteNonQuery(string commandText, params DbParameter[] commandParameters)
+        {
+            return CommonExecute<int>((cmd) => cmd.ExecuteNonQuery(), commandText, commandParameters);
+        }
+
+        public int ExecuteNonQuery(string commandText, DbTransaction transaction,
+            params DbParameter[] commandParameters)
+        {
+            return CommonExecute<int>((cmd) => cmd.ExecuteNonQuery(), commandText, transaction,
+                commandParameters);
+        }
+
+        public int ExecuteNonQuery(DbCommand command)
+        {
+            return CommonExecute<int>((cmd) => cmd.ExecuteNonQuery(), command);
+        }
+
+        #endregion
+
+        #region 查询结果执行Scalar        
+
+        public object ExecuteScalar(string commandText, params DbParameter[] commandParameters)
+        {
+            return CommonExecute<object>((cmd) => cmd.ExecuteScalar(), commandText, commandParameters);
+        }
+
+        public int ExecuteScalar(string commandText, DbTransaction transaction, params DbParameter[] commandParameters)
+        {
+            return CommonExecute<int>((cmd) => cmd.ExecuteNonQuery(), commandText, transaction,
+                commandParameters);
+        }
+
+        public object ExecuteScalar(DbCommand command)
+        {
+            return CommonExecute<object>((cmd) => cmd.ExecuteNonQuery(), command);
+        }
+
+        #endregion
+
+        #region 查询数据结果
+
+        public IDataReader ExecuteReader(string commandText, params DbParameter[] commandParameters)
+        {
+            return CommonExecute<IDataReader>((cmd) => cmd.ExecuteReader(CommandBehavior.CloseConnection),
+                commandText, commandParameters);
+        }
+
+        public IDataReader ExecuteReader(string commandText, DbTransaction transaction,
+            params DbParameter[] commandParameters)
+        {
+            return CommonExecute<IDataReader>((cmd) => cmd.ExecuteReader(CommandBehavior.CloseConnection),
+                commandText, transaction, commandParameters);
+        }
+
+        public IDataReader ExecuteReader(DbCommand command)
+        {
+            return CommonExecute<IDataReader>((cmd) => cmd.ExecuteReader(CommandBehavior.CloseConnection),
+                command);
+        }
+
+        #endregion
+
+        #region 查询数据集结果
+   
+        public DataSet ExecuteDataSet(DbCommand command)
+        {
+            return CommonExecute<DataSet>(InnerExecuteDataSet, command);
+        }
+
+        public DataSet ExecuteDataSet(string commandText, DbTransaction transaction,
+            params DbParameter[] commandParameters)
+        {
+            return CommonExecute<DataSet>(InnerExecuteDataSet, commandText, transaction, commandParameters);
+        }
+
+        public DataSet ExecuteDataSet(string commandText, params DbParameter[] commandParameters)
+        {
+            return CommonExecute<DataSet>(InnerExecuteDataSet, commandText, commandParameters);
+        }
+
+        #endregion
+    }
+}

+ 102 - 0
MBI/SAGA.DotNetUtils/Data.Framework/DatabaseUtil.cs

@@ -0,0 +1,102 @@
+/*-------------------------------------------------------------------------
+ * 功能描述:DatabaseUtil
+ * 作者:xulisong
+ * 创建时间: 2019/2/27 11:05:03
+ * 版本号:v1.0
+ *  -------------------------------------------------------------------------*/
+
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Linq;
+using System.Text;
+using System.Text.RegularExpressions;
+using System.Threading.Tasks;
+
+namespace Saga.Framework.DB
+{
+    public static class DatabaseUtil
+    {
+        #region 验证注入相关信息
+        private static string InjectionMatch { get; set; }
+        private static string GetInjectionMatch()
+        {
+            if (string.IsNullOrWhiteSpace(InjectionMatch))
+            {
+                string[] array = new string[]
+                {
+                    "insert\\s",
+                    "delete\\s",
+                    "update\\s",
+                    "drop\\s",
+                    "truncate\\s",
+                    "exec\\s",
+                    "count\\(",
+                    "declare\\s",
+                    "asc\\(",
+                    "mid\\(",
+                    "char\\(",
+                    "net user",
+                    "xp_cmdshell",
+                    "/add\\s",
+                    "exec master.dbo.xp_cmdshell",
+                    "net localgroup administrators"
+                };
+                string str = ".*(";
+                for (int i = 0; i < array.Length - 1; i++)
+                {
+                    str = str + array[i] + "|";
+                }
+                InjectionMatch = str + array[array.Length - 1] + ").*";
+            }
+
+            return InjectionMatch;
+        } 
+        #endregion
+        /// <summary>
+        /// 判断字符串是否存在注入信息
+        /// </summary>
+        /// <param name="inputStr"></param>
+        /// <returns></returns>
+        public static bool HasInjection(string inputStr)
+        {
+            return !string.IsNullOrEmpty(inputStr) && Regex.IsMatch(inputStr.ToLower(), GetInjectionMatch());
+        }
+        /// <summary>
+        /// 数据类型转换成数据使用类型
+        /// </summary>
+        /// <param name="type"></param>
+        /// <returns></returns>
+        public static DbType ConvertToDbType(Type type)
+        {
+            DbType result;
+            try
+            {
+                if (type.Name.ToLower() == "byte[]")
+                {
+                    result = DbType.Binary;
+                }
+                else
+                {
+                    result = (DbType)Enum.Parse(typeof(DbType), type.Name);
+                }
+            }
+            catch
+            {
+                result = DbType.String;
+            }
+            return result;
+        }
+        /// <summary>
+        /// 验证事物是否合法
+        /// </summary>
+        /// <param name="trans"></param>
+        /// <returns></returns>
+        public static bool ValidateTransaction(IDbTransaction trans)
+        {
+            if (trans == null) throw new ArgumentNullException("transaction");
+            if (trans.Connection == null) throw new ArgumentException("The transaction was rolled back or committed, please provide an open transaction.", "transaction");
+            return true;
+        }
+    }
+}

+ 58 - 0
MBI/SAGA.DotNetUtils/Data.Framework/IDal.cs

@@ -0,0 +1,58 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Data;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Data.Common;
+
+namespace Saga.Framework.DB
+{
+    /*
+     *需要不需要IBaseDal中的Base;
+     * 如果操作过程中,没有使用传入的默认表名称,则不需要定义在接口里,而是以静态方法的形式公开;
+     */
+    public interface IDal<T> where T : new()
+    {
+        #region 查找,查找方式有很多
+        bool ExistByKey(object key);
+        bool ExistByCondition(string condition);
+        T FindByKey(object key);
+
+        T FindSingle(string condition);
+        T FindSingle(string condition, string orderBy);
+        T FindSingle(string condition, string orderBy, params DbParameter[] parameters);
+        List<T> Find(string condition);
+        List<T> Find(string condition, string orderBy);
+        List<T> Find(string condition, string orderBy, params DbParameter[] parameters);
+
+        #endregion
+
+        #region 删除
+        bool DeleteByKey(object key);
+        bool DeleteByCondition(string condition);
+        bool DeleteByKey(object key, DbTransaction trans);
+        bool DeleteByCondition(string condition, DbTransaction trans);
+        #endregion
+
+        #region 添加
+        bool Insert(T t);
+        bool Insert(T t, DbTransaction tran);
+        bool Insert(Hashtable recordFields, DbTransaction trans);
+        object InsertAddGetKey(T t);
+        object InsertAddGetKey(T t, DbTransaction tran);
+        object InsertAddGetKey(Hashtable recordFields, DbTransaction trans);
+        #endregion
+
+        #region 修改
+        bool Update(T obj, object primaryKeyValue);
+        bool Update(T obj, object primaryKeyValue, DbTransaction trans);
+        bool UpdateByCondition(T obj, string condition);
+        bool UpdateByCondition(T obj, string condition, DbTransaction trans);
+        bool UpdateByCondition(Hashtable recordFields, string condition, DbTransaction trans,
+            params DbParameter[] conditionParameters);
+
+        #endregion
+    }
+}

+ 67 - 0
MBI/SAGA.DotNetUtils/Data.Framework/Sqlite/SqliteDal.cs

@@ -0,0 +1,67 @@
+/*-------------------------------------------------------------------------
+ * 功能描述:BaseSqliteDal
+ * 作者:xulisong
+ * 创建时间: 2019/2/27 9:10:32
+ * 版本号:v1.0
+ *  -------------------------------------------------------------------------*/
+
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Data;
+using System.Data.Common;
+using System.Data.SQLite;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Saga.Framework.DB.Sqlite
+{
+    /// <summary>
+    /// Sqlite基础类
+    /// </summary>
+    /// <typeparam name="T"></typeparam>
+    public class SqliteDal<T>: AbstractDal<T>,IDal<T> where T:new ()
+    {     
+        protected override DbParameter CreatePrimaryKeyParameter(object key)
+        {
+            var parameter = new SQLiteParameter(this.PrimaryKey, DatabaseUtil.ConvertToDbType(key.GetType()));
+            parameter.Value = key;
+            return parameter;
+        }
+        public override Database CreateDatabase()
+        {
+            return new SqliteDatabase(ConnectionString);
+        }
+
+        public override  T FindSingle(string condition, string orderBy, params DbParameter[] parameters)
+        {
+            ValidateInput(condition);
+            ValidateInput(orderBy);
+            T result = default(T);
+            string commandText = string.Format("Select * From {0} ", TableName);
+            if (!string.IsNullOrWhiteSpace(condition))
+            {
+                commandText += string.Format("Where {0} ", condition);
+            }
+            if (!string.IsNullOrWhiteSpace(orderBy))
+            {
+                commandText = commandText + " " + orderBy;
+            }
+            else if (!string.IsNullOrWhiteSpace(DefaultSortField))
+            {
+                commandText = commandText + "" + "Order by " + DefaultSortField + " ASC";
+            }
+            commandText = commandText + string.Format("  LIMIT 1");
+            Database database = CreateDatabase();
+            using (IDataReader dataReader = database.ExecuteReader(commandText, parameters))
+            {
+                if (dataReader.Read())
+                {
+                    result = this.ReaderToEntity(dataReader);
+                }
+            }
+            return result;
+        }
+    }
+}

+ 68 - 0
MBI/SAGA.DotNetUtils/Data.Framework/Sqlite/SqliteDatabase.cs

@@ -0,0 +1,68 @@
+/*-------------------------------------------------------------------------
+ * 功能描述:SqliteDatabase
+ * 作者:xulisong
+ * 创建时间: 2019/2/27 9:48:00
+ * 版本号:v1.0
+ *  -------------------------------------------------------------------------*/
+
+using System;
+using System.Data;
+using System.Data.Common;
+using System.Data.SQLite;
+
+namespace Saga.Framework.DB.Sqlite
+{
+    /// <summary>
+    /// SqliteDatabase数据
+    /// </summary>
+    public class SqliteDatabase:Database
+    {
+        public SqliteDatabase(string connectionString):base(connectionString)
+        {
+      
+        }   
+        /// <summary>
+        /// 创建连接信息
+        /// </summary>
+        /// <returns></returns>
+        public override DbConnection CreateConnection()
+        {
+            //SQLiteConnectionStringBuilder builder = new SQLiteConnectionStringBuilder();
+            return new SQLiteConnection(ConnectionString);
+        }
+        /// <summary>
+        /// 创建关联事务
+        /// </summary>
+        /// <returns></returns>
+        public override DbTransaction CreateTransaction()
+        {
+            return CreateConnection().BeginTransaction();
+        }
+        /// <summary>
+        /// 生成相关参数
+        /// </summary>
+        /// <param name="fieldName"></param>
+        /// <param name="value"></param>
+        /// <returns></returns>
+        public override DbParameter CreateParameter(string fieldName,object value)
+        {
+            var parameter = new SQLiteParameter(fieldName, DatabaseUtil.ConvertToDbType(value.GetType()));
+            parameter.Value = value;
+            return parameter;
+        }
+        public override DbParameter CreateParameter(string fieldName,DbType type, object value)
+        {
+            var parameter = new SQLiteParameter(fieldName, type);
+            parameter.Value = value;
+            return parameter;
+        }
+        protected override DataSet InnerExecuteDataSet(DbCommand cmd)
+        {
+            DataSet ds = new DataSet();
+            SQLiteDataAdapter da = new SQLiteDataAdapter(cmd as SQLiteCommand);
+            da.Fill(ds);
+            da.Dispose();
+            return ds;
+        }  
+    }
+}

+ 7 - 0
MBI/SAGA.DotNetUtils/SAGA.DotNetUtils.csproj

@@ -273,6 +273,13 @@
     <Compile Include="Configration\TSZVersion.cs" />
     <Compile Include="Configration\XMLFile.cs" />
     <Compile Include="Configration\XmlManger.cs" />
+    <Compile Include="Data.Framework\AbstractDal.cs" />
+    <Compile Include="Data.Framework\Bll.cs" />
+    <Compile Include="Data.Framework\Database.cs" />
+    <Compile Include="Data.Framework\DatabaseUtil.cs" />
+    <Compile Include="Data.Framework\IDal.cs" />
+    <Compile Include="Data.Framework\Sqlite\SqliteDal.cs" />
+    <Compile Include="Data.Framework\Sqlite\SqliteDatabase.cs" />
     <Compile Include="Data\EdgesArray\EdgesArrayBase.cs" />
     <Compile Include="Data\EdgesArray\DBCollection.cs" />
     <Compile Include="Data\EdgesArray\EAEdge.cs" />

+ 4 - 0
MBI/SAGA.MBIAssistData/SAGA.MBIAssistData.csproj

@@ -68,7 +68,11 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="BLL\DutyBIMRelation.cs" />
+    <Compile Include="BLL\SystemCheckReportBll.cs" />
+    <Compile Include="BLL\SystemCheckResultBll.cs" />
     <Compile Include="DAL\DutyBIMRelation.cs" />
+    <Compile Include="DAL\SystemCheckReportDal.cs" />
+    <Compile Include="DAL\SystemCheckResultDal.cs" />
     <Compile Include="Model\DutyBIMRelation.cs" />
     <Compile Include="Model\SystemCheckReport.cs" />
     <Compile Include="Model\SystemCheckResult.cs" />