Ejemplo n.º 1
0
        /// <summary>排序</summary>
        /// <param name="nowId"></param>
        /// <param name="m">"up" or "down"</param>
        /// <param name="fieldId">id字段名:"ClassId"</param>
        /// <param name="tableName">表名</param>
        /// <param name="strWhere">附加Where : " sid=1 "</param>
        /// <param name="isExistsMoreLv">是否存在多级分类,一级时,请使用false,多级使用true(一级不包括ParentId字段)</param>
        /// <param name="fieldName">字段名:"SortId"</param>
        /// <param name="fieldParentId">字段名:"ParentId"</param>
        public static void AutoSort(int nowId, string m, string fieldId, string tableName, string strWhere = "", bool isExistsMoreLv = false, string fieldName = "SortId", string fieldParentId = "ParentId")
        {
            string ss = "";
            string iOldId = nowId.ToString(), iOldNo = "", iNewId = "", iNewNo = "";
            string[] aRs1, aRs2;
            var select = new SelectHelper();

            if (string.IsNullOrEmpty(strWhere) == false)
            {
                ss = " and ";
            }

            if (isExistsMoreLv)
            {

                var dt =
                    select.ExcuSQLDataTable("select top 1 " + fieldName + "," + fieldParentId + " from " + tableName +
                                            " where " + strWhere + " " + ss + " " + fieldId + "=" + iOldId);
                aRs1 = DataTableHelper.GetColumnsString(dt);
                if (aRs1.Length > 0)
                {
                    iOldNo = aRs1[0];

                    if (string.IsNullOrEmpty(strWhere) == false)
                    {
                        ss = " and " + fieldParentId + "=" + aRs1[1] + " and ";
                    }
                    else
                    {
                        ss = " " + fieldParentId + "=" + aRs1[1] + " and ";
                    }
                }
                else
                {
                    return;
                }
                aRs1 = null;
            }
            else
            {
                iOldNo = select.ExecuteScalar("select top 1 " + fieldName + " from " + tableName + " where " + strWhere + " " + ss + " " + fieldId + "=" + iOldId) + "";
                if (iOldNo.Length == 0) { return; }
            }

            if (m == "up")
            {//上移
                var dt =
                    select.ExcuSQLDataTable("select top 1 " + fieldId + "," + fieldName + " from " + tableName + " where " + strWhere + " " + ss + " " + fieldName + "<" + iOldNo + " order by " + fieldName + " desc");
                aRs2 = DataTableHelper.GetColumnsString(dt);
                if (aRs2.Length > 0)
                {
                    iNewId = aRs2[0];
                    iNewNo = aRs2[1];
                }
                else
                {
                    return;
                }
            }
            else
            {//下移
                var dt =
                    select.ExcuSQLDataTable("select top 1 " + fieldId + "," + fieldName + " from " + tableName + " where " + strWhere + " " + ss + " " + fieldName + ">" + iOldNo + " order by " + fieldName + " asc");
                aRs2 = DataTableHelper.GetColumnsString(dt);
                if (aRs2.Length > 0)
                {
                    iNewId = aRs2[0];
                    iNewNo = aRs2[1];
                }
                else
                {
                    return;
                }
            }
            var update = new UpdateHelper();
            update.Update("update " + tableName + " set " + fieldName + "=" + iNewNo + " where " + strWhere + " " + ss + " " + fieldId + "=" + iOldId);
            update.Update("update " + tableName + " set " + fieldName + "=" + iOldNo + " where " + strWhere + " " + ss + " " + fieldId + "=" + iNewId);
        }
Ejemplo n.º 2
0
        /// <summary>自动排序</summary>
        /// <param name="fieldId">id字段名:"Id"</param>
        /// <param name="tableName">表名:"NewsClass"</param>
        /// <param name="strWhere">附加Where : " sid=1 "</param>
        /// <param name="isExistsMoreLv">是否存在多级分类,一级时,请使用false,多级使用true,(一级不包括ParentId字段)</param>
        /// <param name="pid">父级分类的ParentId</param>
        /// <param name="fieldName">字段名:"OrderId"</param>
        /// <param name="fieldParentId">字段名:"ParentId"</param>
        public static bool AutoSort(string fieldId, string tableName, string strWhere = "", bool isExistsMoreLv = false, int pid = 0, string fieldName = "Sort", string fieldParentId = "ParentId")
        {
            try
            {
                string sw = "";
                var select = new SelectHelper();
                var update = new UpdateHelper();

                if (string.IsNullOrEmpty(strWhere) == false)
                {
                    sw = " where " + strWhere;
                }

                if (isExistsMoreLv)
                {
                    pid = ConvertHelper.Cint0(pid);

                    if (string.IsNullOrEmpty(sw))
                    {
                        sw = " where " + fieldParentId + "=" + pid;
                    }
                    else
                    {
                        sw += " and " + fieldParentId + "=" + pid;
                    }
                }

                var dt =
                    select.ExcuSQLDataTable("select " + fieldId + " from " + tableName + " " + sw + " order by " + fieldName + " asc," + fieldId);
                string[] pRs = DataTableHelper.GetArrayString(dt, fieldId);

                if (pRs.Length > 0)
                {
                    int ti = pRs.Length;

                    for (int i = 0; i < ti; i++)
                    {
                        if (pRs[i].Length > 0)
                        {
                            update.Update("update " + tableName + " set " + fieldName + "=" + (i + 1).ToString() + " where " + fieldId + "=" + pRs[i]);

                            if (isExistsMoreLv)
                            {
                                AutoSort(fieldId, tableName, strWhere, isExistsMoreLv, ConvertHelper.Cint0(pRs[i]), fieldName, fieldParentId);
                            }
                        }
                    }
                }
                pRs = null;

                return true;
            }
            catch
            {
                return false;
            }
        }