我想实现的功能是这样的,就是有一个父类,父类下面有子类,子类下面还有子类;我要删除那个父类是就把他下面的两个子类就一起删除!!但是这些类别还不在一个数据表里面,父类和第一个子类在一个表kclist里,另一个子类在kczy数据表里。
private void dg1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{ conn.Open();
SqlCommand comm = new SqlCommand();
SqlTransaction trans;
trans = conn.BeginTransaction();
comm.Connection = conn;
comm.Transaction = trans;
try
{
string id = this.dg1.DataKeys[e.Item.ItemIndex].ToString();//
comm.CommandText = "delete from kclist where id="+ id;//删除父类 comm.ExecuteNonQuery();
comm.CommandText = "delete from kclist where ParentID= "+e.Item.Cells[0].Text;//删除第一个子类,
comm.ExecuteNonQuery();
comm.CommandText = "delete from kczy where Context= "+e.Item.Cells[1].Text;删除第二个子类。
comm.ExecuteNonQuery();
trans.Commit();
this.Response.Write("<script>alert('删除成功 !!')</script>");
}
catch(Exception)
{
trans.Rollback();
this.Response.Write("<script>alert('删除失败!已回滚!')</script>");
}
finally
{
conn.Close();
BindToDg1();
}
}
private void dg1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{ conn.Open();
SqlCommand comm = new SqlCommand();
SqlTransaction trans;
trans = conn.BeginTransaction();
comm.Connection = conn;
comm.Transaction = trans;
try
{
string id = this.dg1.DataKeys[e.Item.ItemIndex].ToString();//
comm.CommandText = "delete from kclist where id="+ id;//删除父类 comm.ExecuteNonQuery();
comm.CommandText = "delete from kclist where ParentID= "+e.Item.Cells[0].Text;//删除第一个子类,
comm.ExecuteNonQuery();
comm.CommandText = "delete from kczy where Context= "+e.Item.Cells[1].Text;删除第二个子类。
comm.ExecuteNonQuery();
trans.Commit();
this.Response.Write("<script>alert('删除成功 !!')</script>");
}
catch(Exception)
{
trans.Rollback();
this.Response.Write("<script>alert('删除失败!已回滚!')</script>");
}
finally
{
conn.Close();
BindToDg1();
}
}
出现什么问题了????
先删除子类,后删除父类
comm.CommandText = "delete from kczy where Context= "+e.Item.Cells[1].Text;
comm.ExecuteNonQuery();
comm.CommandText = "delete from kclist where ParentID= "+e.Item.Cells[0].Text;
comm.ExecuteNonQuery();
string id = this.dg1.DataKeys[e.Item.ItemIndex].ToString();
comm.CommandText = "delete from kclist where id="+ id;
// comm.CommandText = "delete from kclist where id="+ e.Item.Cells[0].Text;
comm.ExecuteNonQuery();
这样也不可以删除阿, 还是回滚阿,