数据结构:
数据层删除类 
dal.Delete 
public void Delete(int cate_id) 
{ StringBuilder strSql=new StringBuilder(); strSql.Append("delete from Gcategory "); 
strSql.Append(" where cate_id=@cate_id "); 
SqlParameter[] parameters = { 
new SqlParameter("@cate_id", SqlDbType.Int,4)}; 
parameters[0].Value = cate_id; DbHelperSQL.ExecuteSql(strSql.ToString(),parameters); 

业务逻辑层 bllGcategory.Delete 
public void Delete(int cate_id) 
{ dal.Delete(cate_id); 
} 表示层 删除啊按钮触发事件 
if (e.CommandName == "Delete") 

int iUser_id = Convert.ToInt32((e.Item.FindControl("txtCate_id") as TextBox).Text.Trim()); 
bllGcategory.Delete(iUser_id); 
BindData(); 
} 现在的处理结果是,父类删除后,子类仍然会保留在数据库中。 如何能在父类删除的同时,相应的子类也删除呢?

解决方案 »

  1.   

    既然你写sql语句,不会连inner join都不知道滴。
      

  2.   

    数据库就是那样啊--parent_id是父类 cate_id是子类 无父类时parent_id为0
      

  3.   


    /// <summary>
            /// 执行多条SQL语句,实现数据库事务。
            /// </summary>
            /// <param name="SQLStringList">多条SQL语句</param>
            public static int ExecuteSqlTran(List<String> SQLStringList)
            {
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    SqlTransaction tx = conn.BeginTransaction();
                    cmd.Transaction = tx;
                    try
                    {
                        int count = 0;
                        for (int n = 0; n < SQLStringList.Count; n++)
                        {
                            string strsql = SQLStringList[n];
                            if (strsql.Trim().Length > 1)
                            {
                                cmd.CommandText = strsql;
                                count += cmd.ExecuteNonQuery();
                            }
                        }
                        tx.Commit();
                        return count;
                    }
                    catch
                    {
                        tx.Rollback();
                        return 0;
                    }
                }
            }
      

  4.   


    你这压根儿就是sql问题不是程序问题……
    parent_id、cate_id是不是在一张表?他们是以什么关系存储的?
    删除的需求是什么?是根据parent_id删除父类和所有子类还是根据cate_id删除父类和所有子类?
      

  5.   

    是同一张表 那不是有张图吗!存储的时候没有关系 根据cate_id删除某一类时,parent_id=cate_id的对应子类也删除
      

  6.   


    首先写一个inner join查询,删除你所说的“子类”记录。然后下一个查询删除你所说的“父类”记录。这两个sql语句在一个 SqlTrsanction 内执行。
      

  7.   

    你可以移到sql server板块去学学sql语句怎么写。一个删除(你所说的)“子类”记录的语句类似于:delete a from [子类] a inner join [父类] b on a.xxxx=b.yyy where b.查询条件
      

  8.   

    图看不到的select parent_id from Gcategory where cate_id=@cate_id 
    delete from Gcategory where parent_id =@parent_id 我猜的……
      

  9.   

    这样只能删除一条记录..cate_id是唯一的
      

  10.   

    虽然麻烦 不过这样处理是可以解决的
    if (e.CommandName == "Delete")
      {
      int iUser_id = Convert.ToInt32((e.Item.FindControl("txtCate_id") as TextBox).Text.Trim());
      //int iUser_id = Convert.ToInt32(this.dlGcategory1.DataKeys[e.Item.ItemIndex].ToString());
      bllGcategory.Delete(iUser_id);
      DataSet dsGcategory3=bllGcategory.GetSubByParent(iUser_id);//获取子类列表
      //删除子类
      if(dsGcategory3.Tables.Count>0)
      {
      for(int i=0;i<dsGcategory3.Tables[0].Rows.Count;i++)
      {
      int cate_id = Convert.ToInt32(dsGcategory3.Tables[0].Rows[i]["cate_id"].ToString());
      bllGcategory.Delete(cate_id);  }
      }  BindData();
      }
      }