Ejemplo n.º 1
0
        /// <summary>
        /// 生成用于更新的Sql命令
        /// </summary>
        public static DbCommand CreateUpdateMemberToNullCommand <T>(Database db, GenericWhereEntity <T> whereEntity,
                                                                    List <string> memberNames)
        {
            var dbColumnNames = EntityMappingTool.GetDbColumnNames(typeof(T), memberNames);

            //生成Sql语句
            StringBuilder sqlBuilder = new StringBuilder();

            sqlBuilder.AppendFormat("UPDATE {0} SET", whereEntity.TableName);
            for (int i = 0; i < dbColumnNames.Count; i++)
            {
                sqlBuilder.Append((i == 0) ? "" : ",");
                sqlBuilder.AppendFormat("{0}.[{1}]=null", whereEntity.TableName, dbColumnNames[i]);
            }

            //WHERE
            string whereSql = SqlCreator.CreateWhereSql(whereEntity);

            sqlBuilder.Append(" ").Append(whereSql);

            //参数
            DbCommand cmd = db.GetSqlStringCommand(sqlBuilder.ToString());

            FillSqlParameters(db, cmd, whereEntity);

            return(cmd);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 创建用于删除的Sql命令
        /// </summary>
        public static DbCommand CreatDeleteCommand <T>(Database db, GenericWhereEntity <T> whereEntity)
        {
            //生成Sql语句
            StringBuilder sqlBuilder = new StringBuilder();

            sqlBuilder.AppendFormat("DELETE ", EntityMappingTool.GetDbTableName(typeof(T)));
            sqlBuilder.Append(SqlCreator.CreateWhereSql(whereEntity));
            DbCommand cmd = db.GetSqlStringCommand(sqlBuilder.ToString());

            FillSqlParameters(db, cmd, whereEntity);
            return(cmd);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 生成用于更新的Sql命令
        /// </summary>
        public static DbCommand CreateUpdateCommand <T>(Database db, T entity, GenericWhereEntity <T> whereEntity)
        {
            Type entityType = typeof(T);

            List <string>       notNullEntityFields    = EntityInstanceTool.GetNotNullFields(entity);
            List <string>       notNullDbCloumnNames   = EntityMappingTool.GetDbColumnNames(entityType, notNullEntityFields);
            List <DbType>       notNullDbColumnTypes   = EntityMappingTool.GetDbColumnTypes(entityType, notNullEntityFields);
            List <PropertyInfo> notNullEntityPropertys = EntityInstanceTool.GetNotNullEntityPropertys(entity);

            //生成Sql语句
            List <string> parameterIndex = new List <string>();
            StringBuilder sqlBuilder     = new StringBuilder();

            sqlBuilder.AppendFormat("UPDATE {0} SET ", whereEntity.TableName);
            bool firstColumn = true;

            for (int i = 0; i < notNullDbCloumnNames.Count; i++)
            {
                string loopColumn = notNullDbCloumnNames[i];
                sqlBuilder.Append(firstColumn ? "" : ",");
                firstColumn = false;
                sqlBuilder.AppendFormat("{0}.[{1}]=@{1}", whereEntity.TableName, loopColumn);
                parameterIndex.Add(loopColumn);
            }

            //WHERE
            string whereSql = SqlCreator.CreateWhereSql(whereEntity);

            sqlBuilder.Append(" ").Append(whereSql);

            //参数
            DbCommand cmd = db.GetSqlStringCommand(sqlBuilder.ToString());

            for (int i = 0; i < notNullDbCloumnNames.Count; i++)
            {
                db.AddInParameter(cmd, "@" + notNullDbCloumnNames[i], notNullDbColumnTypes[i],
                                  notNullEntityPropertys[i].GetValue(entity, null));
            }
            FillSqlParameters(db, cmd, whereEntity);

            return(cmd);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 生成用于更新的Sql命令
        /// </summary>
        public static DbCommand CreateUpdateMemberToNullCommand <T>(Database db, GenericWhereEntity <T> whereEntity,
                                                                    string memberName)
        {
            string dbColumnName = EntityMappingTool.GetDbColumnName(typeof(T), memberName);

            //生成Sql语句
            StringBuilder sqlBuilder = new StringBuilder();

            sqlBuilder.AppendFormat("UPDATE {0} SET  {0}.[{1}]=null", whereEntity.TableName, dbColumnName);

            //WHERE
            string whereSql = SqlCreator.CreateWhereSql(whereEntity);

            sqlBuilder.Append(" ").Append(whereSql);

            //参数
            DbCommand cmd = db.GetSqlStringCommand(sqlBuilder.ToString());

            FillSqlParameters(db, cmd, whereEntity);

            return(cmd);
        }