【IT168 技术文档】最近项目里遇到这样的一个小技术问题,新闻可以任意按客户的需要进行排序,需要实现的效果图如下:
由于各种类别输入了蛮多测试信息,有几百条数据,一个个把排序码都生成也很麻烦,看能否没有什么好办法能生成不重复的,唯一排序码?达到如下效果?
总不可能人工的一个个输入吧? 也没那个耐心的,还是想想有什么好办法吧?
其实很快就想到了 ROW_NUMBER() 功能,用这个,应该是可行的,折腾了一小会儿,这个SQL语句就写好了,给大家分享一下,有需要的朋友们可以直接拿过来就可以用了,虽然也没什么难的,但是自己写总会折腾一会儿,直接复制过去修改一下,省心省事一些。
UPDATE BASE_NEWS
SET SortCode = NEWS.SortCode
FROM (SELECT ROW_NUMBER() OVER (ORDER BY CreateDate DESC) + 10000000 AS SortCode
, Id
FROM BASE_NEWS) AS NEWS
WHERE NEWS.Id = BASE_NEWS.Id
SET SortCode = NEWS.SortCode
FROM (SELECT ROW_NUMBER() OVER (ORDER BY CreateDate DESC) + 10000000 AS SortCode
, Id
FROM BASE_NEWS) AS NEWS
WHERE NEWS.Id = BASE_NEWS.Id
此文虽然是过于基础了一些,但是往往基础的东西,大家日常生活里需要得更多,希望能对需要的人起点儿参考作用,可以COPY一下就可以用了.
若有谁可以写得更好,也可以贴出来分享一下。
四个按钮中的代码实现可以参考一下
#region private void SetSort(string function, int index) 设置排序顺序
/// <summary>
/// 设置排序顺序
/// </summary>
/// <param name="function">排序动作</param>
/// <param name="index">索引</param>
private void SetSort(string function, int index)
{
string id = this.gridView.DataKeys[index].Value.ToString();
string targetId = string.Empty;
try
{
this.DbHelper.Open();
// 判断编辑权限
// this.CheckPermission(this.CategoryId, OperationCode.Edit);
switch (function)
{
case BaseDbSortLogic.CommandSetTop:
if (index > 0)
{
BaseDbSortLogic.SetTop(this.DbHelper, ProjectTable.TableName, id);
this.gridView.SelectedIndex = 0;
}
break;
case BaseDbSortLogic.CommandSetUp:
if (index > 0)
{
targetId = this.gridView.DataKeys[index - 1].Value.ToString();
this.gridView.SelectedIndex = index - 1;
BaseDbSortLogic.Swap(this.DbHelper, ProjectTable.TableName, id, targetId);
}
break;
case BaseDbSortLogic.CommandSetDown:
if ((index + 2) < this.gridView.Rows.Count)
{
targetId = this.gridView.DataKeys[index + 1].Value.ToString();
this.gridView.SelectedIndex = index + 1;
BaseDbSortLogic.Swap(this.DbHelper, ProjectTable.TableName, id, targetId);
}
break;
case BaseDbSortLogic.CommandSetBottom:
if ((index + 2) < this.gridView.Rows.Count)
{
BaseDbSortLogic.SetBottom(this.DbHelper, ProjectTable.TableName, id);
this.gridView.SelectedIndex = this.gridView.Rows.Count - 2;
}
break;
}
// 获取列表
this.DoSearch();
}
catch (Exception ex)
{
this.LogException(ex);
throw ex;
}
finally
{
this.DbHelper.Close();
}
}
#endregion
/// <summary>
/// 设置排序顺序
/// </summary>
/// <param name="function">排序动作</param>
/// <param name="index">索引</param>
private void SetSort(string function, int index)
{
string id = this.gridView.DataKeys[index].Value.ToString();
string targetId = string.Empty;
try
{
this.DbHelper.Open();
// 判断编辑权限
// this.CheckPermission(this.CategoryId, OperationCode.Edit);
switch (function)
{
case BaseDbSortLogic.CommandSetTop:
if (index > 0)
{
BaseDbSortLogic.SetTop(this.DbHelper, ProjectTable.TableName, id);
this.gridView.SelectedIndex = 0;
}
break;
case BaseDbSortLogic.CommandSetUp:
if (index > 0)
{
targetId = this.gridView.DataKeys[index - 1].Value.ToString();
this.gridView.SelectedIndex = index - 1;
BaseDbSortLogic.Swap(this.DbHelper, ProjectTable.TableName, id, targetId);
}
break;
case BaseDbSortLogic.CommandSetDown:
if ((index + 2) < this.gridView.Rows.Count)
{
targetId = this.gridView.DataKeys[index + 1].Value.ToString();
this.gridView.SelectedIndex = index + 1;
BaseDbSortLogic.Swap(this.DbHelper, ProjectTable.TableName, id, targetId);
}
break;
case BaseDbSortLogic.CommandSetBottom:
if ((index + 2) < this.gridView.Rows.Count)
{
BaseDbSortLogic.SetBottom(this.DbHelper, ProjectTable.TableName, id);
this.gridView.SelectedIndex = this.gridView.Rows.Count - 2;
}
break;
}
// 获取列表
this.DoSearch();
}
catch (Exception ex)
{
this.LogException(ex);
throw ex;
}
finally
{
this.DbHelper.Close();
}
}
#endregion