Exemple #1
0
        /// <summary>
        /// 生成Update语句
        /// </summary>
        /// <param name="updateObject"></param>
        /// <param name="command"></param>
        public static void TranslateIntoUpdate <T>(Update <T> update, SqlCommand command)
        {
            StringBuilder sqlUpdate = new StringBuilder();

            sqlUpdate.Append(baseUpdate);
            sqlUpdate.Append(update.TableName + " SET ");

            //删除字段
            foreach (var item in update.ExcludeField)
            {
                if (update.Values.ContainsKey(item.Name))
                {
                    update.Values.Remove(item.Name);
                }
            }
            //条件列也可能跟新
            //foreach (var item in update.Criterions)
            //{
            //    if (update.Vaules.ContainsKey(item.FieldName))
            //    {
            //        update.Vaules.Remove(item.FieldName);
            //    }
            //}

            foreach (var item in update.Values)
            {
                sqlUpdate.Append(item.Key + "=@U_" + item.Key + ",");
                command.Parameters.Add(ParameterDataNullHelper.ChangeNull("@U_" + item.Key, item.Value));
            }
            sqlUpdate.Remove(sqlUpdate.Length - 1, 1);

            TranslateHelper.GetCriterionString(sqlUpdate, update.Criterions, command, update.SqlOperator);

            command.CommandText = sqlUpdate.ToString();
        }
        /// <summary>
        /// 生成Delete语句
        /// </summary>
        /// <param name="deleteObject"></param>
        /// <param name="command"></param>
        public static void TranslateIntoDelete(Delete delete, SqlCommand command)
        {
            StringBuilder deleteString = new StringBuilder();

            deleteString.Append(baseDelete);
            deleteString.Append(delete.TableName);

            TranslateHelper.GetCriterionString(deleteString, delete.Criterions, command, delete.SqlOperator);

            command.CommandText = deleteString.ToString();
        }
Exemple #3
0
        /// <summary>
        /// 生成查询语句
        /// </summary>
        /// <param name="query"></param>
        /// <param name="command"></param>
        public static void TranslateIntoSelect(Query query, SqlCommand command)
        {
            StringBuilder selectString = new StringBuilder();

            selectString.Append(baseSelect);
            selectString.Append(query.TableName);

            TranslateHelper.GetCriterionString(selectString, query.Criterions, command, query.SqlOperator);

            if (query.OrderByClauses.Count() > 0)
            {
                selectString.Append(TranslateHelper.GetStringFromOrderByClause(query.OrderByClauses));
            }

            command.CommandText = selectString.ToString();
        }
Exemple #4
0
        public static void TranslateIntoComplexQuery(ComplexQuery complexQuery, SqlCommand command)
        {
            string        baseString = "SELECT ";
            StringBuilder result     = new StringBuilder();

            result.Append(baseString);

            if (complexQuery.IsDictinct == true)
            {
                result.Append("DISTINCT ");
            }

            if (complexQuery.TopNumber != 0)
            {
                result.Append(TranslateHelper.GetTopNumber(complexQuery.TopNumber));
            }

            foreach (var item in complexQuery.NeedFields)
            {
                result.Append(TranslateHelper.GetFieldString(item));
            }
            result.Remove(result.Length - 1, 1);

            result.Append(TranslateHelper.GetJoinConditionString(complexQuery.NeedFields, complexQuery.JoinCriterion));

            if (complexQuery.Criterions.Count() > 0)
            {
                TranslateHelper.GetCriterionString(result, complexQuery.Criterions, command, complexQuery.SqlOperator);
            }

            if (complexQuery.OrderByClauses.Count > 0)
            {
                result.Append(TranslateHelper.GetStringFromOrderByClause(complexQuery.OrderByClauses));
            }

            command.CommandText = result.ToString();
        }