判斷記錄是否存在的通用方法

在數據寫入到數據庫時,一般要根據某些條件檢測記錄是否存在,而後決定是否要寫入,數據寫入分兩種情形:新添加數據和更新數據,在通用權限底層中,對這類判斷進行了封裝。前端

位置截圖

主要核心代碼部分

        /// <summary>
        /// 判斷存在
        /// </summary>
        /// <param name="parameters"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual bool Exists(List<KeyValuePair<string, object>> parameters, object id = null)
        {
            bool result = false;
            if (id != null)
            {
                result = DbLogic.Exists(DbHelper, this.CurrentTableName, parameters, new KeyValuePair<string, object>(this.PrimaryKey, id));
            }
            else
            {
                result = DbLogic.Exists(DbHelper, this.CurrentTableName, parameters);
            }
            return result;
        }

        /// <summary>
        /// 記錄是否存在
        /// </summary>
        /// <param name="dbHelper">數據庫鏈接</param>
        /// <param name="tableName">目標表名</param>
        /// <param name="parameters">參數</param>
        /// <param name="targetField">獲取字段</param>
        /// <param name="name">目標字段名</param>
        /// <param name="parameters">目標字段值</param>
        /// <returns>存在</returns>
        public static bool Exists(IDbHelper dbHelper, string tableName, List<KeyValuePair<string, object>> parameters, KeyValuePair<string, object> parameter = new KeyValuePair<string, object>())
        {
            return GetCount(dbHelper, tableName, parameters, parameter) > 0;
        }
        /// <summary>
        /// 獲取個數
        /// </summary>
        /// <param name="dbHelper">數據庫鏈接</param>
        /// <param name="tableName">目標表名</param>
        /// <param name="parameters">目標字段,值</param>
        /// <returns>行數</returns>
        public static int GetCount(IDbHelper dbHelper, string tableName, List<KeyValuePair<string, object>> parameters, KeyValuePair<string, object> parameter = new KeyValuePair<string, object>())
        {
            int result = 0;
            string sqlQuery = "SELECT COUNT(1) "
                + " FROM " + tableName
                + " WHERE " + GetWhereString(dbHelper, parameters, BaseBusinessLogic.SQLLogicConditional);

            if (!string.IsNullOrEmpty(parameter.Key))
            {
                if (parameter.Value != null)
                {
                    sqlQuery += BaseBusinessLogic.SQLLogicConditional + "( " + parameter.Key + " <> '" + parameter.Value + "' ) ";
                }
                else
                {
                    sqlQuery += BaseBusinessLogic.SQLLogicConditional + "( " + parameter.Key + " IS NOT NULL) ";
                }
            }

            object returnObject = null;
            if (parameters != null)
            {
                returnObject = dbHelper.ExecuteScalar(sqlQuery, dbHelper.MakeParameters(parameters));
            }
            else
            {
                returnObject = dbHelper.ExecuteScalar(sqlQuery);
            }
            if (returnObject != null)
            {
                result = int.Parse(returnObject.ToString());
            }
            return result;
        }


該方法支持多種數據庫(Oracle、MySQL、MSSQL等),id爲記錄的主鍵,新增時是null。能夠參照引入到本身項目中。sql

方法應用舉例

如在新增公司時,要求公司編號和公司名稱不能夠重複,前端文本框值變化時使用了Ajax異步判斷。數據庫

        /// <summary>
        /// 驗證公司編號是否重複
        /// </summary>
        /// <param name="code"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        private bool ValidateCode(string code, string id)
        {
            code = DbLogic.SqlSafe(code);
            id = DbLogic.SqlSafe(id);
            return !organizeManager.Exists(new KeyValuePair<string, object>(BaseOrganizeEntity.FieldCode, code), id);
        }
        /// <summary>
        /// 驗證公司名稱是否重複
        /// </summary>
        /// <param name="name"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        private bool ValidateName(string name, string id)
        {
            name = DbLogic.SqlSafe(name);
            id = DbLogic.SqlSafe(id);
            return !organizeManager.Exists(new KeyValuePair<string, object>(BaseOrganizeEntity.FieldFullName, name), id);
        }

在新增公司時,Id爲null,更新時傳入公司的主鍵Id。異步

有了這個方法,表中記錄存在判斷處理就方便多了,並且支持增長多種條件的限制判斷(參數List<KeyValuePair<string, object>> parameters)。this

相關文章
相關標籤/搜索