数据结构:
数据层删除类
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();
} 现在的处理结果是,父类删除后,子类仍然会保留在数据库中。 如何能在父类删除的同时,相应的子类也删除呢?
数据层删除类
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();
} 现在的处理结果是,父类删除后,子类仍然会保留在数据库中。 如何能在父类删除的同时,相应的子类也删除呢?
/// <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;
}
}
}
你这压根儿就是sql问题不是程序问题……
parent_id、cate_id是不是在一张表?他们是以什么关系存储的?
删除的需求是什么?是根据parent_id删除父类和所有子类还是根据cate_id删除父类和所有子类?
首先写一个inner join查询,删除你所说的“子类”记录。然后下一个查询删除你所说的“父类”记录。这两个sql语句在一个 SqlTrsanction 内执行。
delete from Gcategory where parent_id =@parent_id 我猜的……
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();
}
}