读书人

用上移和下移的连接来移动记录进行排序

发布时间: 2012-01-22 22:38:43 作者: rapoo

用上移和下移的连接来移动记录进行排序
//################################### 排序(向上) ###################################
if (active == "up ")
{
pid = Convert.ToInt32(Qz.Data.Gn.Getsql(Request[ "p_id "]));
xpid = pid - 1;


Qz.Data.Reader dr = new Qz.Data.Reader( "SELECT rootid FROM product WHERE p_id= " + pid);

if (dr.Read())
{
RootID = Convert.ToInt32(dr[ "rootid "]);
}
dr.Close();
Qz.Data.Reader dr2 = new Qz.Data.Reader( "SELECT rootid FROM product WHERE p_id= " + xpid);
if (dr2.Read())
{
XRootID = Convert.ToInt32(dr2[ "rootid "]);
}
dr2.Close();


if (RootID > 1)
{
Qz.Data.Data.ExecuteNonQuery( "UPDATE product SET rootid= " + RootID + " WHERE p_id= " + xpid + " ");
Qz.Data.Data.ExecuteNonQuery( "UPDATE product SET rootid= " + XRootID + " WHERE p_id= " + pid + " ");



Response.Redirect( "qz_cp_gl.aspx ");

}

}
//################################### 排序(向下) ###################################
if (active == "down ")
{

pid = Convert.ToInt32(Qz.Data.Gn.Getsql(Request[ "p_id "]));
xpid = pid + 1;


Qz.Data.Reader dr = new Qz.Data.Reader( "SELECT rootid FROM product WHERE p_id= " + pid);

if (dr.Read())
{
RootID = Convert.ToInt32(dr[ "rootid "]);
}
dr.Close();
Qz.Data.Reader dr2 = new Qz.Data.Reader( "SELECT rootid FROM product WHERE p_id= " + xpid);
if (dr2.Read())
{
XRootID = Convert.ToInt32(dr2[ "rootid "]);
}
dr2.Close();


if (RootID > 1)
{
Qz.Data.Data.ExecuteNonQuery( "UPDATE product SET rootid= " + RootID + " WHERE p_id= " + xpid + " ");


Qz.Data.Data.ExecuteNonQuery( "UPDATE product SET rootid= " + XRootID + " WHERE p_id= " + pid + " ");

Response.Redirect( "qz_cp_gl.aspx ");

}
}


p_id 是主键,rootid是排序的字段!
如:
p_id rootid
11 1
12 2
18 3
19 4
让移动的那个产品的rootid和上面的记录或者下面的记录交换rootid值,可是我写的这个程序还是有问题,大家帮忙看看怎么改进更好!还有就是第一个和最后一个如何处理?

[解决办法]
粘段代码你参考一下:
if(e.CommandName == "up ")
{
OleDbDataReader dr = conn.Reader( "select cindex,BigClassID from news where cid= ' "+cid+ " ' ");
if(dr.Read())
{
cindex = Convert.ToInt32(dr.GetValue(0).ToString());
string BigClassID = Convert.ToString(dr.GetValue(1).ToString());
int findex;
if(cindex == 1)
{
findex=0;
}
else
{
findex=cindex-1;
}
if(findex> 0)
{
bool isupdate1 = conn.ExecuteSql( "update news set cindex= "+cindex+ " where BigClassID= ' "+BigClassID+ " ' and cindex= "+findex);
bool isupdate2 = conn.ExecuteSql( "update news set cindex= "+findex+ " where BigClassID= ' "+BigClassID+ " ' and cid= ' "+cid+ " ' ");
if(!isupdate1 || !isupdate2)
{
Response.Write( " <script language=javascript> alert( '数据未修改,数据库操作有错误! '); ");
Response.Write( " </script> ");
}
else
{
Response.Write( " <script language=javascript> ");
Response.Write( "window.location.href= 'admin_info.aspx?csort= "+Request.QueryString[ "csort "]+ " '; </script> ");
}
}
}
else
{
Response.Write( " <SCRIPT language=JavaScript> alert( '非法字段或无效数值! '); ");
Response.Write( " </script> ");
Response.End();
}
}
if(e.CommandName == "down ")
{
OleDbDataReader dr = conn.Reader( "select cindex,BigClassID from news where cid= ' "+cid+ " ' ");
if(dr.Read())
{
cindex=Convert.ToInt32(dr.GetValue(0).ToString());
string BigClassID = Convert.ToString(dr.GetValue(1).ToString());
int eindex;
OleDbDataReader edr = conn.Reader( "select top 1 cindex from news where BigClassID= ' "+BigClassID+ " ' and cindex> "+cindex+ " order by cindex ");
if(edr.Read())
{
eindex=Convert.ToInt32(edr.GetValue(0).ToString());
}
else
{
eindex=9999;
}
if(eindex!=9999)
{
bool isupdate1 = conn.ExecuteSql( "update news set cindex= "+cindex+ " where BigClassID= ' "+BigClassID+ " ' and cindex= "+eindex);


bool isupdate2 = conn.ExecuteSql( "update news set cindex= "+eindex+ " where BigClassID= ' "+BigClassID+ " ' and cid= ' "+cid+ " ' ");
if(!isupdate1 || !isupdate2)
{
Response.Write( " <script language=javascript> alert( '数据未修改,数据库操作有错误! '); ");
Response.Write( " </script> ");
}
else
{
Response.Write( " <script language=javascript> ");
Response.Write( "window.location.href= 'admin_info.aspx?csort= "+Request.QueryString[ "csort "]+ " '; </script> ");
}
}
}
else
{
Response.Write( " <SCRIPT language=JavaScript> alert( '非法字段或无效数值! '); ");
Response.Write( " </script> ");
Response.End();
}
}

[解决办法]
在ItemCommand事件里写这段代码就行.大致就是在数据库里多一个字段,我看你的思路是对的,就选的哪条记录与(下上)最近的这个字段值进行处理.

读书人网 >asp.net

热点推荐