巧用 ROW_NUMBER() 生成不重复的唯一排序码,按客户的需要任意排序数据
2010-09-28 14:39 通用C#系统架构 阅读(4609) 评论(32) 编辑 收藏 举报最近项目里遇到这样的一个小技术问题,新闻可以任意按客户的需要进行排序,需要实现的效果图如下:
由于各种类别输入了蛮多测试信息,有几百条数据,一个个把排序码都生成也很麻烦,看能否没有什么好办法能生成不重复的,唯一排序码?达到如下效果?
总不可能人工的一个个输入吧? 也没那个耐心的,还是想想有什么好办法吧?
其实很快就想到了 ROW_NUMBER() 功能,用这个,应该是可行的,折腾了一小会儿,这个SQL语句就写好了,给大家分享一下,有需要的朋友们可以直接拿过来就可以用了,虽然也没什么难的,但是自己写总会折腾一会儿,直接复制过去修改一下,省心省事一些。
1 UPDATE BASE_NEWS
2 SET SortCode = NEWS.SortCode
3 FROM (SELECT ROW_NUMBER() OVER (ORDER BY CreateDate DESC) + 10000000 AS SortCode
4 , Id
5 FROM BASE_NEWS) AS NEWS
6 WHERE NEWS.Id = BASE_NEWS.Id
2 SET SortCode = NEWS.SortCode
3 FROM (SELECT ROW_NUMBER() OVER (ORDER BY CreateDate DESC) + 10000000 AS SortCode
4 , Id
5 FROM BASE_NEWS) AS NEWS
6 WHERE NEWS.Id = BASE_NEWS.Id
此文虽然是过于基础了一些,但是往往基础的东西,大家日常生活里需要得更多,希望能对需要的人起点儿参考作用,可以COPY一下就可以用了.
若有谁可以写得更好,也可以贴出来分享一下。
四个按钮中的代码实现可以参考一下
代码
1 #region private void SetSort(string function, int index) 设置排序顺序
2 /// <summary>
3 /// 设置排序顺序
4 /// </summary>
5 /// <param name="function">排序动作</param>
6 /// <param name="index">索引</param>
7 private void SetSort(string function, int index)
8 {
9 string id = this.gridView.DataKeys[index].Value.ToString();
10 string targetId = string.Empty;
11 try
12 {
13 this.DbHelper.Open();
14 // 判断编辑权限
15 // this.CheckPermission(this.CategoryId, OperationCode.Edit);
16 switch (function)
17 {
18 case BaseDbSortLogic.CommandSetTop:
19 if (index > 0)
20 {
21 BaseDbSortLogic.SetTop(this.DbHelper, ProjectTable.TableName, id);
22 this.gridView.SelectedIndex = 0;
23 }
24 break;
25 case BaseDbSortLogic.CommandSetUp:
26 if (index > 0)
27 {
28 targetId = this.gridView.DataKeys[index - 1].Value.ToString();
29 this.gridView.SelectedIndex = index - 1;
30 BaseDbSortLogic.Swap(this.DbHelper, ProjectTable.TableName, id, targetId);
31 }
32 break;
33 case BaseDbSortLogic.CommandSetDown:
34 if ((index + 2) < this.gridView.Rows.Count)
35 {
36 targetId = this.gridView.DataKeys[index + 1].Value.ToString();
37 this.gridView.SelectedIndex = index + 1;
38 BaseDbSortLogic.Swap(this.DbHelper, ProjectTable.TableName, id, targetId);
39 }
40 break;
41 case BaseDbSortLogic.CommandSetBottom:
42 if ((index + 2) < this.gridView.Rows.Count)
43 {
44 BaseDbSortLogic.SetBottom(this.DbHelper, ProjectTable.TableName, id);
45 this.gridView.SelectedIndex = this.gridView.Rows.Count - 2;
46 }
47 break;
48 }
49 // 获取列表
50 this.DoSearch();
51 }
52 catch (Exception ex)
53 {
54 this.LogException(ex);
55 throw ex;
56 }
57 finally
58 {
59 this.DbHelper.Close();
60 }
61 }
62 #endregion
2 /// <summary>
3 /// 设置排序顺序
4 /// </summary>
5 /// <param name="function">排序动作</param>
6 /// <param name="index">索引</param>
7 private void SetSort(string function, int index)
8 {
9 string id = this.gridView.DataKeys[index].Value.ToString();
10 string targetId = string.Empty;
11 try
12 {
13 this.DbHelper.Open();
14 // 判断编辑权限
15 // this.CheckPermission(this.CategoryId, OperationCode.Edit);
16 switch (function)
17 {
18 case BaseDbSortLogic.CommandSetTop:
19 if (index > 0)
20 {
21 BaseDbSortLogic.SetTop(this.DbHelper, ProjectTable.TableName, id);
22 this.gridView.SelectedIndex = 0;
23 }
24 break;
25 case BaseDbSortLogic.CommandSetUp:
26 if (index > 0)
27 {
28 targetId = this.gridView.DataKeys[index - 1].Value.ToString();
29 this.gridView.SelectedIndex = index - 1;
30 BaseDbSortLogic.Swap(this.DbHelper, ProjectTable.TableName, id, targetId);
31 }
32 break;
33 case BaseDbSortLogic.CommandSetDown:
34 if ((index + 2) < this.gridView.Rows.Count)
35 {
36 targetId = this.gridView.DataKeys[index + 1].Value.ToString();
37 this.gridView.SelectedIndex = index + 1;
38 BaseDbSortLogic.Swap(this.DbHelper, ProjectTable.TableName, id, targetId);
39 }
40 break;
41 case BaseDbSortLogic.CommandSetBottom:
42 if ((index + 2) < this.gridView.Rows.Count)
43 {
44 BaseDbSortLogic.SetBottom(this.DbHelper, ProjectTable.TableName, id);
45 this.gridView.SelectedIndex = this.gridView.Rows.Count - 2;
46 }
47 break;
48 }
49 // 获取列表
50 this.DoSearch();
51 }
52 catch (Exception ex)
53 {
54 this.LogException(ex);
55 throw ex;
56 }
57 finally
58 {
59 this.DbHelper.Close();
60 }
61 }
62 #endregion
将权限管理、工作流管理做到我能力的极致,一个人只能做好那么很少的几件事情。