TO: islet
依你的方法,问题已解决,用SELECT嵌套,的确省去了很多中间过程,谢谢。
我的完整代码如下:
private void upBtn_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
if(NewsKindList.SelectedIndex > 0)
{
int nSelectedIndex = NewsKindList.SelectedIndex;
String id = NewsKindList.SelectedItem.Value.ToString();
SqlConnection myConnection = new SqlConnection(GlobalDB.DataBaseDB.ConnectionString);
String cmdText = "UPDATE NewsKinds SET KindOrder = KindOrder - 1 WHERE NewsKindID = " + id;
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
cmdText = "UPDATE NewsKinds SET KindOrder = KindOrder + 1 WHERE KindOrder = (SELECT KindOrder FROM NewsKinds WHERE NewsKindID = " + id + ") AND NewsKindID <> " + id;
myCommand = new SqlCommand(cmdText,myConnection);
myCommand.ExecuteNonQuery();
myConnection.Close();
BindNewsKindData();
NewsKindList.SelectedIndex = Math.Max(0,(nSelectedIndex - 1));
}
}
private void downBtn_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
if(NewsKindList.SelectedIndex < (NewsKindList.Items.Count - 1))
{
int nSelectedIndex = NewsKindList.SelectedIndex;
String id = NewsKindList.SelectedItem.Value.ToString();
SqlConnection myConnection = new SqlConnection(GlobalDB.DataBaseDB.ConnectionString);
String cmdText = "UPDATE NewsKinds SET KindOrder = KindOrder + 1 Where NewsKindID = " + id;
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
cmdText = "UPDATE NewsKinds SET KindOrder = KindOrder -1 WHERE KindOrder = (SELECT KindOrder FROM NewsKinds WHERE NewsKindID = " + id + ") AND NewsKindID <> " + id;
myCommand = new SqlCommand(cmdText,myConnection);
myCommand.ExecuteNonQuery();
myConnection.Close();
BindNewsKindData();
NewsKindList.SelectedIndex = Math.Min((NewsKindList.Items.Count - 1),(nSelectedIndex + 1));
}
}