前兩天需要實現一個上移下移功能,就上網上搜了一下,沒想到這個簡單的小功能也沒有搜到特別理想的結果,只有自己動手豐衣足食了,雖然簡單,但是希望對大家有所幫助。
功能:實現任意有排序字段表單的上移和下移,並可以按分類來操作排序。
首先是存儲過程:
寫的稍微有些繁瑣,可能是半夜太困了吧
1 ALTER PROCEDURE [dbo].[Proc_Common_UpDown]
2
3
4 @Sign int = 0, -- 0: 上移 1:下移
5 @TableName nvarchar(50), -- 表名
6 @ItemName nvarchar(50), -- 主鍵字段名
7 @ItemID int, -- 主鍵ID
8 @SortName nvarchar(50), -- 排序ID
9 @TypeName nvarchar(50)='', -- 分類字段名
10 @TypeValue nvarchar(50)= '' -- 分類值
11
12 AS
13
14 BEGIN
15 SET NOCOUNT ON
16
17 DECLARE
18 @SQL nvarchar(4000),
19 @ThisSort int, -- 當前ID
20 @PREVID int, -- 前一個ID
21 @NextID int, -- 後一個ID
22 @Count int
23
24 --臨時索引表--
25 CREATE TABLE #Tab
26 (
27 ItemID int,
28 Sort int
29 )
30 SET @SQL = 'INSERT INTO #Tab (ItemID,Sort) SELECT '
31 +@ItemName+','+@SortName+' FROM '+@TableName
32
33 IF (@TypeName<>'' AND @TypeValue<>'') SET @SQL = @SQL+' WHERE '+@TypeName+'='+@TypeValue
34 SET @SQL = @SQL+' ORDER BY Sort ASC '
35 EXEC(@SQL)
36 SET @SQL = ''
37
38 SELECT @Count = COUNT(*) FROM #Tab
39 SELECT @ThisSort = Sort FROM #Tab WHERE ItemID = @ItemID
40 IF(@ThisSort>1) SELECT @PREVID = ItemID FROM #Tab WHERE Sort=(@ThisSort-1)
41 IF(@ThisSort<@Count) SELECT @NextID = ItemID FROM #Tab WHERE Sort=(@ThisSort+1)
42
43 IF(@Sign=0)
44 BEGIN
45 IF(@ThisSort>1)
46 BEGIN
47 SET @SQL = 'UPDATE '+@TableName+' SET '+@SortName+'='+CONVERT(varchar(100),(@ThisSort-1))+' WHERE '+@ItemName+' = '+CONVERT(varchar(100),@ItemID) + ';'
48 + 'UPDATE '+@TableName+' SET '+@SortName+'='+CONVERT(varchar(100),@ThisSort)+' WHERE '+@ItemName+' = '+CONVERT(varchar(100),@PREVID)
49
50 END
51 END
52 ELSE
53 BEGIN
54 IF(@ThisSort<@Count)
55 BEGIN
56 SET @SQL = 'UPDATE '+@TableName+' SET '+@SortName+'='+CONVERT(varchar(100),(@ThisSort+1))+' WHERE '+@ItemName+' = '+CONVERT(varchar(100),@ItemID) + ';'
57 + 'UPDATE '+@TableName+' SET '+@SortName+'='+CONVERT(varchar(100),@ThisSort)+' WHERE '+@ItemName+' = '+CONVERT(varchar(100),@NextID)
58 END
59 END
60
61 EXEC(@SQL)
62
63 END
64
數據層的公共類:
public class Common
{
public static Database db = DatabaseFactory.CreateDatabase("Huoqk_CMS");
/// <summary>
/// 上移下移
/// </summary>
/// <param name="Sign">移動方向:0 上移, 1下移</param>
/// <param name="TableName">表名</param>
/// <param name="ItemName">主鍵名</param>
/// <param name="ItemID">主鍵ID</param>
/// <param name="SortName">排序字段</param>
/// <param name="TypeName">類型名</param>
/// <param name="TypeValue">類型ID</param>
public static void UPDown(int Sign, string TableName, string ItemName, int ItemID, string SortName, string TypeName, string TypeValue)
{
string spName = "Proc_Common_UpDown";
DbCommand dbCommand = db.GetStoredProcCommand(spName);
db.AddInParameter(dbCommand, "Sign", DbType.Int32, Sign);
db.AddInParameter(dbCommand, "TableName", DbType.String,TableName);
db.AddInParameter(dbCommand, "ItemName", DbType.String, ItemName);
db.AddInParameter(dbCommand, "ItemID", DbType.Int32, ItemID);
db.AddInParameter(dbCommand, "SortName", DbType.String, SortName);
db.AddInParameter(dbCommand, "TypeName", DbType.String, TypeName);
db.AddInParameter(dbCommand, "TypeValue", DbType.String, TypeValue);
db.ExecuteNonQuery(dbCommand);
}
}
數據層(DAL)具體的調用:CMS_Channel 表的排序功能
public void UPDown(int sign, int itemid)
{
Common.UPDown(sign, "CMS_Channel", "CID", itemid, "Sort", "", "");
}
至於業務邏輯層或者Web層的調用基本就很簡單了:
UPDown(111, 0);上移
UPDown(111, 1);下移