예제 #1
0
        /// <summary>
        /// 根据类型生成Update 语句 必须输入条件
        /// </summary>
        /// <typeparam name="T">数据类型。</typeparam>
        /// <param name="conditions">条件 "例如:ID=@ID"</param>
        /// <param name="keyName">条件 主键列一般是不能更新的 所以要过滤掉。</param>
        /// <returns></returns>
        public static string GetUpdateCommandStringByType <T>(string conditions, string keyName) where T : class
        {
            if (string.IsNullOrEmpty(conditions) || string.IsNullOrWhiteSpace(conditions))
            {  //防止误操作 更新所有数据~
                throw new Exception("为保证数据库安全,更新语句必须提供更新范围条件!");
            }
            PropertyInfo[] properties = DapperPagerUtils.GetPropertyInfosByType(typeof(T)).ToArray();
            var            columns    = properties.Where(o => !o.Name.Equals(keyName)).Select(p => p.Name).ToArray();
            var            parameters = columns.Select(name => "[" + name + "]" + "=@" + name).ToList();

            string str = String.Format(updateString, typeof(T).Name, String.Join(",", parameters));

            //if (!string.IsNullOrEmpty(keyName))
            //{
            //    str = str.Replace("@" + keyName + ",", "").Replace(keyName + "=", "");
            //}

            if (string.IsNullOrEmpty(conditions))
            {
                return(str);
            }
            else
            {
                return(str + " AND " + conditions);
            }
        }
예제 #2
0
        /// <summary>
        /// 生成查询 WITH NO LOCK 语句 此模式在高并发下允许脏读 (相当于READ UNCOMMITTED)无法保证数据的一致性
        /// 但是对性能较好。高并发情况下推荐使用此方法。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static string GetSelectWithNoLockCommandStringByType <T>(string conditions = "") where T : class
        {
            PropertyInfo[] properties = DapperPagerUtils.GetPropertyInfosByType(typeof(T)).ToArray();
            var            columns    = properties.Select(p => "" + p.Name + "").ToArray();
            string         str        = string.Format(selectStringWithNoLock, String.Join(",", columns), typeof(T).Name);

            if (string.IsNullOrEmpty(conditions))
            {
                return(str);
            }
            else
            {
                return(str + " AND " + conditions);
            }
        }
예제 #3
0
        /// <summary>
        /// 生成过滤掉标识列的SQL插入语句命令
        /// </summary>
        /// <typeparam name="T">泛型类名</typeparam>
        /// <param name="keyName">标识列名称</param>
        /// <returns></returns>
        public static string GetInsertCommandFilteredIdentityColumn <T>(string keyName) where T : class
        {
            PropertyInfo[] properties = DapperPagerUtils.GetPropertyInfosByType(typeof(T)).ToArray();
            if (properties.Count() < 1)
            {
                throw new Exception("传入对象不具有属性,请检查传递对象!");
            }
            var columnsCommand = properties.Where(o => !o.Name.Equals(keyName)).Select(p => "[" + p.Name + "]").ToArray();
            var columnsValue   = properties.Where(o => !o.Name.Equals(keyName)).Select(p => p.Name).ToArray();

            return(string.Format(insertString,
                                 typeof(T).Name,
                                 String.Join(",", columnsCommand), properties[0].Name,
                                 String.Join(",@", columnsValue)));
        }